Posted: Thu Dec 14, 2006 9:19 pm Post subject: C++ Compile error with classes and objects.
So, I'm still not great with C++, so I'm doing a bunch of pratice programs to get into the language. The one I'm doing now is a MadLibs generator/reader.
I have just started it' so there is very little content to it, but I already have an error with the object I'm using:
main.cpp:38: request for member `WriteLib' in `madLibsHandle()', which is of non-aggregate type `MadLibs ()()'
Posted: Fri Dec 15, 2006 12:05 am Post subject: Re: C++ Compile error with classes and objects.
Looks like you are doing this:
c++:
int foo(void){} int main(void){ foo.bar(); }
Craige wrote:
main.cpp:38: request for member `WriteLib' in `madLibsHandle()', which is of non-aggregate type `MadLibs ()()'
... and that your compiler ought to print the type better.
md
Posted: Fri Dec 15, 2006 1:00 am Post subject: (No subject)
It would be nice to see all of the code; debugging from 1 error and ~4 lines of real code is quite hard.
OneOffDriveByPoster
Posted: Fri Dec 15, 2006 9:26 am Post subject: (No subject)
md wrote:
It would be nice to see all of the code; debugging from 1 error and ~4 lines of real code is quite hard.
Looks like all the code you need is there though. The guy was pretty good about that.
c++:
/* 14: */ MadLibs madLibsHandle();
md
Posted: Fri Dec 15, 2006 9:41 am Post subject: (No subject)
I don't see a declaration for MadLibs, which by the error seems to be being treated like a class with a member function WriteLib(); WriteLib) however is just declared as a function, not as part of a class.
More code is definitely needed though, because even though you seem to have hit upon the right answer there could be other equally bad things in there as well... and I like looking at code
wtd
Posted: Fri Dec 15, 2006 9:52 am Post subject: (No subject)
I'd wager a guess that md's right and there's a whole lot of funkiness going on here that we're just not seeing. But I could be wrong.
Post all of the code!
r.3volved
Posted: Fri Dec 15, 2006 12:14 pm Post subject: Re: C++ Compile error with classes and objects.
Craige wrote:
main.cpp:38: request for member `WriteLib' in `madLibsHandle()', which is of non-aggregate type `MadLibs ()()'
Your declaration of your object doesn't need brackets unless you're passing something to the constructor.
ex.
Creating a string...
code:
//String is an object-class and you make an instance of a string object
string mystring;
//Make an instance of string instantiated with "Hello World"
string mystring( "Hello World" );
//Allocating memory for type string
string* mystring = new string();
//Allocating memory for type string instantiated with "Hello World"
string* mystring = new string( "Hello World" );
Sponsor Sponsor
Craige
Posted: Fri Dec 15, 2006 12:23 pm Post subject: (No subject)
I'm at school right now, just about to go to Co-Op. I'll post the whole code tonight.
The WriteLib method though is not neccessaraly a just a print function(method). It takes entire controll for the program, for that action, saving a bundle of code in main, and making things easier to read. What's wrong with that?
wtd
Posted: Fri Dec 15, 2006 12:33 pm Post subject: (No subject)
Well, you see, methods should be kept as absolutely simple as possible. It certainly sounds as though your WriteLib method is doing too much, though I won't be able to tell for sure until I see all of the code.
Please don't take this too hard. It takes a long time to learn how to use objects. It takes even longer to learn when to use objects and how to use them correctly.
As for:
code:
MadLibs madLibsHandle();
If you are trying to declare an object of type MadLibs here, then you should know that C++ compilers will see this as a function declaration. To declare an object, and have it initialized by its default constructor, you should simply write:
code:
MadLibs madLibsHandle;
Craige
Posted: Fri Dec 15, 2006 7:15 pm Post subject: (No subject)
wtd wrote:
If you are trying to declare an object of type MadLibs here, then you should know that C++ compilers will see this as a function declaration. To declare an object, and have it initialized by its default constructor, you should simply write:
code:
MadLibs madLibsHandle;
That was the problem. Guess I read the book wrong
I'll post the code anyway. Like I said, I just started it the other day, so it's just the base design, but here you go anyway:
int main (int argc, char *argv)
{
MadLibs madLibsHandle;
cout << "####################################################\n"<<
"# MadLibs CPP 0.0.1 #\n"<<
"# Craige Leeder #\n"<<
"# December 13, 2006 #\n"<<
"# This is a simple MadLibs program coded in C++ to #\n"<<
"# pracice features of the language. Enjoy! #\n"<<
"# #\n"<<
"####################################################\n\n";
while ( command != 'q' && command != 'Q' )
{
cout << "What would you like to do?\n"<<
"--------------\n";
cout << "w - Write New MadLib\n"<<
"a - Answer Saved MadLib\n"<<
"q - Quit\n";
command = getchar();
cout << "\n";
if ( command == 'w' )
{
madLibsHandle.WriteLib();
}
else if ( command == 'a' )
{
madLibsHandle.ReadLib();
}
}
return 0;
}
madlibs.h
code:
using namespace std;
class MadLibs
{
private:
ifstream rfile;
ofstream wfile;
I know my headers arn't organized like they should be. It's something I have to look into to see better how they work across files.
wtd
Posted: Fri Dec 15, 2006 7:27 pm Post subject: (No subject)
First off, a matter of style.
code:
cout << "####################################################" << endl
<< "# MadLibs CPP 0.0.1 #" << endl
<< "# Craige Leeder #" << endl
<< "# December 13, 2006 #" << endl
<< "# This is a simple MadLibs program coded in C++ to #" << endl
<< "# pracice features of the language. Enjoy! #" << endl
<< "# #" << endl
<< "####################################################" << endl
<< endl;
Now, there are a lot of issues with this code. A few:
Please don't use C strings. C++ has a perfectly nice string class.
You have the following:
code:
class MadLibs
{
private:
ifstream rfile;
ofstream wfile;
Now, your MadLibs class has no constructors, default or otherwise. How do these variables get initialized? In all likelihood, they do not belong at this scope. Rather they should be locally scoped within a function.
Craige
Posted: Fri Dec 15, 2006 7:42 pm Post subject: (No subject)
wtd wrote:
First off, a matter of style.
code:
cout << "####################################################" << endl
<< "# MadLibs CPP 0.0.1 #" << endl
<< "# Craige Leeder #" << endl
<< "# December 13, 2006 #" << endl
<< "# This is a simple MadLibs program coded in C++ to #" << endl
<< "# pracice features of the language. Enjoy! #" << endl
<< "# #" << endl
<< "####################################################" << endl
<< endl;
Now, there are a lot of issues with this code. A few:
Please don't use C strings. C++ has a perfectly nice string class.
While I respect your input on that matter, I would like to say that there is nothing wrong with the C style strings, nor the \n character. There are plenty of programmers who use them by choice in all their C++ programs.
Quote:
You have the following:
code:
class MadLibs
{
private:
ifstream rfile;
ofstream wfile;
Now, your MadLibs class has no constructors, default or otherwise. How do these variables get initialized? In all likelihood, they do not belong at this scope. Rather they should be locally scoped within a function.
The reason for that is because the class is just being designed. I hadn't wrote a constructor yet. I said when I posted that it was just in the beginning stages. I was just trying to get method definitions complete and check the compile to be sure it worked, and beings I wasn't planing on having any data passed to the constructor, it was not necessary for the compile, and I left it out for the time being. There will be a constructor later.
wtd
Posted: Fri Dec 15, 2006 7:44 pm Post subject: (No subject)
Craige wrote:
While I respect your input on that matter, I would like to say that there is nothing wrong with the C style strings, nor the \n character. There are plenty of programmers who use them by choice in all their C++ programs.
They are idiots.
No, there's no more gentle way to put it than that.
md
Posted: Fri Dec 15, 2006 7:56 pm Post subject: (No subject)
C-style strings are horrible. They are the single most common source of run-time errors and buffer over-run exploits. std::string provides such an unbelievably better string that there really isn't any good reason to be using a C string, no matter what.