Re: [suse-programming-e] Registering another class as a variable in one class (Beginner Question).
Dear Paul.
I am still confused.
Sorry I am a really beginner in GNU C++.
1. May I make 2 classes in one file?
2. header is somewhat like this : "#include
You need to pre-declare the class samba.
Just moving it above the main method should do the trick, but you might want to declare it in a header file for safety.
Paul
Dear my friends..
I am practicing GNU C++ on SuSE 9.1.
But if I compile my code I got this error message as below: ==== patrixlinux@patrix:~/arsip/proyek/gcc/liss> g++ -o liss ./liss.cpp In file included from /usr/include/g++/backward/iostream.h:31, from liss.cpp:1: /usr/include/g++/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of
C++ standard. Examples include substituting the <X> header for the
header for C++ includes, or <sstream> instead of the deprecated header . To disable this warning use -Wno-deprecated. liss.cpp: In member function `void liss.cpp:19: error: `samba' undeclared (first use
function) liss.cpp:19: error: (Each undeclared identifier is reported only once for each function it appears in.) liss.cpp:19: error: `s' undeclared (first use this function) liss.cpp:19: error: syntax error before `(' token liss.cpp: At global scope: liss.cpp:39: error: `struct samba' used prior to declaration patrixlinux@patrix:~/arsip/proyek/gcc/liss> ====
Why is it so?
Thank you very much in advance.
ps.Sorry for my stupid question. ==== Here is my code. ==== #include
#include #include class menu { public: char pilihan; void tampil();
};
void menu::tampil(){ for (;;){ cout << "Menu : (S)amba (P)ostfix (B)ind (Q)uit\n"; cin >> pilihan; switch ( toupper(pilihan) ){ case 'S': cout << "You are administrating Samba Server" << endl; samba * s = new samba(); s->tampil();
break; case 'P': cout << "You are administrating Postfix Server" << endl; break; case 'B': cout << "You are administrating BIND Server" << endl; break; case 'Q': cout << "You selected Quit. Good bye!" << endl; exit(0); default: cout << "***No such command!***" << endl; break; } } }
class samba { public: char resource[50]; void tampil(); };
void samba::tampil(){ for(;;){ cout << "Share file or directory: "; cin >> resource; } }
int main(){ menu * m = new menu(); m->tampil(); return 0; }
__________________________________ Do you Yahoo!? Yahoo! Mail - 50x more storage than other
On Thu, 30 Sep 2004 05:16:01 -0700 (PDT), Prabu Subroto
wrote: the menu::tampil()': this providers! http://promotions.yahoo.com/new_mail
-- To unsubscribe, email: suse-programming-e-unsubscribe@suse.com For additional commands, email: suse-programming-e-help@suse.com Archives can be found at: http://lists.suse.com/archive/suse-programming-e
_______________________________ Do you Yahoo!? Declare Yourself - Register online to vote today! http://vote.yahoo.com
On Thursday 30 September 2004 21:16, Prabu Subroto wrote:
I am still confused. Sorry I am a really beginner in GNU C++. 1. May I make 2 classes in one file?
Yes.
2. header is somewhat like this : "#include
.
In C++, drop .h for system headers -- it's the newer standard. You can still append .h, but gcc will warn you.
I am confused how should the header file of my "menu" class for registering "samba" class in the "menu" class?
Could you give me an example of the header file of my "menu" class please....?
Class "menu" needs "samba", but you define "samba" after "menu". One way is as Paul suggested: class samba {...}; class menu {...}; The other whould be to declare samba first: class samba; class menu {...}; class samba {...};
Hi, As an experienced 'C' programmer but C++ novice I am wondering what is the best programming practice for standard input/output when programming C++, should one use 'printf()' and 'sprintf()' or 'cout' and 'cin'? Or is it down to personal preference? Thanks Kirk Garvey
Kirk Garvey wrote:
Hi,
As an experienced 'C' programmer but C++ novice I am wondering what is the best programming practice for standard input/output when programming C++, should one use 'printf()' and 'sprintf()' or 'cout' and 'cin'? Or is it down to personal preference?
Personally I use both. When I just want to simply print unformatted data I use cout but if I have much formating, especially if part of a line is formated significantly different than another part I find printf much easier. Along the same lines I have found that for reading data that has a complex format that scanf is easier than cin. I have also noticed that some C programmers aren't even aware of the power of the scanf and printf. I'm not as expert with cin and cout as I am with printf and scanf but one time I wrote code to read in a data file in both C and C++ and found that the scanf did it in one scan statement where with cin I had to read the whole line into a buffer and do significant processing to store the values. If anyone is interested I can post the code and they can show me how it could be done easier in C++ -- Chris W Not getting the gifts you want? The Wish Zone can help. http://thewishzone.com
Thanks for your response Chris W. On Friday 01 October 2004 03:40, you wrote:
significant processing to store the values. If anyone is interested I can post the code and they can show me how it could be done easier in C++
I for one would certainly be interested in seeing the code, it could serve as an example when to choose one I/O method over another. Regards Kirk
Kirk Garvey wrote:
I for one would certainly be interested in seeing the code, it could serve as an example when to choose one I/O method over another.
Ok first the scanf method struct record{ char city[4]; char date[5]; char time[4]; float AirTemp; float WindSpeed; float WindDirection; float RainFall; }; int main() { char c; FILE *out; FILE *in; out = fopen("prog1.new", "wb"); in = fopen("prog1.dat", "rt"); record r; /*structure to store data to be written to binary file*/ while(fscanf(in, "\"%4s\",\"%5s\",%4s,%f,%f,%f,%f,\n", &r.city, &r.date, &r.time, &r.AirTemp, &r.WindSpeed, &r.WindDirection, &r.RainFall)!= EOF){ fwrite(&r, sizeof(record), 1, out); } return 0; } Then the C++ which I am sure could be much better since I knew very little about when I wrote this. int main() { FILE *out; /*binary file to be created*/ out = fopen("prog1.bin", "wb"); string line; /*memory to store data from text file one line at a time*/ record WeatherData; /*structure to store data to be written to binary file*/ ifstream in("prog1.dat"); /*input stream to read text file*/ while(in){ in >> line; strncpy(WeatherData.city, (line.substr(1,4)).c_str(), 4); strncpy(WeatherData.date, (line.substr(8,5)).c_str(), 5); strncpy(WeatherData.time, (line.substr(15,4)).c_str(), 4); line = line.substr(20); int begin = 0; int end = line.find(','); WeatherData.AirTemp = atof((line.substr(begin, end - begin)).c_str()); begin = ++end; end = line.find(',', end); WeatherData.WindSpeed = atof((line.substr(begin, end - begin)).c_str()); begin = ++end; end = line.find(',', end); WeatherData.WindDirection = atof((line.substr(begin, end - begin)).c_str()); begin = ++end; end = line.find(',', end); WeatherData.RainFall = atof((line.substr(begin, end - begin)).c_str()); fwrite(&WeatherData, sizeof(record), 1, out); } return 0; } Finally some sample data. . . "WASH","01/17",1745,0.0,0.0,0.0,0.00, "CAMA","01/17",1030,25.9,1.8,254.0,0.00, "CAMA","01/17",0945,26.2,0.4,323.0,0.00, "CAMA","01/17",1100,26.2,0.9,107.0,0.00, "SEIL","01/17",1030,26.2,2.2,152.0,0.00, "SEIL","01/17",1045,26.2,3.1,206.0,0.00, "CAMA","01/17",0930,26.4,0.2,331.0,0.00, "CAMA","01/17",0900,26.4,1.1,226.0,0.00, "CAMA","01/17",1015,26.4,1.3,305.0,0.00, "BUFF","01/17",0800,26.4,3.1,30.0,0.00, "CAMA","01/17",1000,26.6,1.1,6.0,0.00, -- Chris W Not getting the gifts you want? The Wish Zone can help. http://thewishzone.com
On Fri, 1 Oct 2004 03:22:07 +0200
Kirk Garvey
Hi,
As an experienced 'C' programmer but C++ novice I am wondering what is the best programming practice for standard input/output when programming C++, should one use 'printf()' and 'sprintf()' or 'cout' and 'cin'? Or is it down to personal preference? IMHO, when programming in C++ one should use C++, not C. The same goes for malloc and new. However, there is nothing really wrong with using the C standard IO.
--
Jerry Feldman
Yupiii...........!!!
Thanks Cincai.... It runs properly now.
Now, I feel free to do my further practice with GNU
C++.
I bought GNU C++ for Linux (by Tom Swan). I am goint
to learn from this book.
Your help in my first step is very usefull.
I have modified my code. I moved the classes into
header file, do "include "bsamba.h" (and also for the
other header file) and compile it again with "g++ -I
./ liss.cpp". and everything as expected.
Thanks Cincai.... Thank you.
--- Cincai Patron
On Thursday 30 September 2004 21:16, Prabu Subroto wrote:
I am still confused. Sorry I am a really beginner in GNU C++. 1. May I make 2 classes in one file?
Yes.
2. header is somewhat like this : "#include
. In C++, drop .h for system headers -- it's the newer standard. You can still append .h, but gcc will warn you.
I am confused how should the header file of my "menu" class for registering "samba" class in the "menu" class?
Could you give me an example of the header file of my "menu" class please....?
Class "menu" needs "samba", but you define "samba" after "menu". One way is as Paul suggested: class samba {...}; class menu {...};
The other whould be to declare samba first: class samba; class menu {...}; class samba {...};
-- To unsubscribe, email: suse-programming-e-unsubscribe@suse.com For additional commands, email: suse-programming-e-help@suse.com Archives can be found at: http://lists.suse.com/archive/suse-programming-e
_______________________________ Do you Yahoo!? Declare Yourself - Register online to vote today! http://vote.yahoo.com
On Mon, 4 Oct 2004 06:11:50 -0700 (PDT)
Prabu Subroto
I have modified my code. I moved the classes into header file, do "include "bsamba.h" (and also for the other header file) and compile it again with "g++ -I ./ liss.cpp". and everything as expected. You do not need to use -I to look into your local directory. #include <iostream> #include "localstuff"
You use angle brackets to include header files in the standard
locations. You use double quotes to include header files in the local
directory.
The '-I ./' is certainly ok to use.
--
Jerry Feldman
Prabu Subroto wrote:
I am still confused. Sorry I am a really beginner in GNU C++. 1. May I make 2 classes in one file?
Yes.
2. header is somewhat like this : "#include
.
Typically you would use
#include<iostream>
rather than
#include
I am confused how should the header file of my "menu" class for registering "samba" class in the "menu" class?
Could you give me an example of the header file of my "menu" class please....?
Typical structure: samba.h: #include<iostream> #ifndef SAMBAH #define SAMBAH class samba { public: samba(); // more class declarations }; #endif samba.cc #include"samba.h" samba::samba(){ // initialisation } // other class definitions. menu.h: #include<iostream> #include"samba.h" #ifndef MENUH #define MENUH class menu { public: menu(); // more class declarations }; #endif etc. ++ How does this work? The samba.h (and menu.h) classes _declare_ the class, but don't _define_ its functions (except maybe some inline ones): that is the .h files don't contain anything like samba::samba(){ ... } This is important because you're only allowed to define a function once. But you can declare it as many times as you like. The #ifndef SAMBAH #define SAMBAH ... #endif part isn't strictly necessary but stops the compiler from reading a header file more than once. It's actually OK to read it more than once because it constains only declarations and not definitions, but compilation will be faster if the compiler doesn't make repeated checks for consistency. menu.h #includes samba.h and so gets to know about the samba class (without introducing any definitions) Typically you compile this bit by bit: g++ samba.cc -c compiles the samba class to samba.o g++ menu.cc -c compiles the menu class to menu.o This becomes advantageous when the whole program is big because you don't have to recompile all the code in the individual .o files when you make a small change. Put it all together with something like g++ -o program main.cc samba.o menu.o where main.cc is the file containing main(). -- JDL
On Thu, 30 Sep 2004 19:38:40 +0100
John Lamb
In practice there are two ways to use the namespace:
1. (java-style) Always qualify standard library functions with std:: as in
std::cout << "Hello." << std::endl
I prefer this style.
2. (C-style) Use the using directive to make the standard library available whenever you want it. The typical way to do this is to start a file
#include<iostream> using namespace std;
Then any standard library function becomes available globally without the std:: qualifier.
You can also make the standard library available locally, e.g.
double function( double x ){ // OK using namespace std; return x + sqrt( x ); }
double another_function( double x ){ // error - no function sqrt() in current scope. return x - sqrt( x ); } <snip> I'm glad your raised namespaces John. There are actually 3 ways to use namespaces. using std::cout; using std::cin; using std::endl;
Most C++ "experts" advise that "using namespace std" is not a good
practice.
The bottom line is when learning a new language, you should try to use
the best practices to avoid getting into bad habits.
--
Jerry Feldman
On Thu, 30 Sep 2004 06:16:10 -0700 (PDT)
Prabu Subroto
Dear Paul.
I am still confused. Sorry I am a really beginner in GNU C++. You really need to get a good basic C++ book based upon the latest C++ standard. -- Jerry Feldman
Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
On 3 Oct 2004 at 9:00, Jerry Feldman wrote:
Date sent: Sun, 3 Oct 2004 09:00:18 -0400
From: Jerry Feldman
On Thu, 30 Sep 2004 06:16:10 -0700 (PDT) Prabu Subroto
wrote: Dear Paul.
I am still confused. Sorry I am a really beginner in GNU C++.
You really need to get a good basic C++ book based upon the latest C++ standard. -- Jerry Feldman
Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
Jerry is right, Prabu, it is very difficult to learn a language by asking questions on the list when your compiler gives you an error message. If you want suggestions for a book, have a look at the reviews section on the ACCU web site http://www.accu.org/ Alan Lenton - Declaration of interest, I sometimes review books for the ACCU :) -- http://www.ibgames.net/alan Registered Linux user #6822 http://counter.li.org Winding Down - Weekly Tech Newsletter - subscribe at http://www.ibgames.net/alan/winding/mailing.html
participants (7)
-
alan@ibgames.com
-
Chris W
-
Cincai Patron
-
Jerry Feldman
-
John Lamb
-
Kirk Garvey
-
Prabu Subroto