I "Confused" My Compiler!?

Started by
19 comments, last by JBourrie 17 years, 10 months ago
Quote:Original post by jpetrie
EDIT: Nevermind.


That's some ugly code. The use of char* for strings in C++ is, as you mentioned, particularly nasty. The most obvious think I see is that you need to qualify objects in the std namespace (std::cout) or insert a "using std::cout" or "using namespace std" near the top someplace. Unless stocks.h has it... but that would be evil as well.

Use std::string unless your teacher/professor has explicitly forbid it (or explicitly required char*... in which case change the return type of GetMonth() to const char* at least).


Yeah...I really had this one messed up, which is why I'm not even gonna attempt to get it done in time for tommorow. I was just wondering why all the errors were happening in this header file, and why I "confused" my compiler to the point that it "bailed" on me.
Originality is dead.
Advertisement
Quote:Original post by Paulius Maruska
For a start - put some newlines at the end of your "stocks.h" file.
Unlike Visual C++, gcc has some problems if there is no newline symbols at the end of included header file (had some problems with this issue myself).


Meh..I usually just ignore that little bit. It's just a warning and doesn't really change the compilation. Just a bit more to read while debugging.

Originality is dead.
Quote:Original post by jpetrie
What does your header file look like?


Like this: (YES I KNOW, REALLY HORRIBLE CODING!)

[SOURCE]#ifndef STOCKS_H#define STOCKS_Hstruct stock{	char symbol[5];	struct Data	{		float open;		float high;		float low;		float close;	} data[744]; //31 Slots Per Month};struct date {int day, month, year;};int SymCheck(char * sym);    /* Pre-Conditions:	 *  Needs 5-Digit Stock Symbol	 *	 * Post-Conditions:	 *  Returns 1 if symbol doesn't work	 *  Returns 0 if symbol does work	 */struct stock & LoadData(char * sym, struct stock &x);	/* Pre-Conditions:	 *   Needs 4-Digit Stock Symbol	 *   Needs Reference to stock struct	 *	 * Post-Conditions:	 *   Returns reference to created struct	 */int DateConv(struct date & d);	/* Pre-Conditions:	 *  Date Struct to convert	 *	 * Post-Conditions:	 *  Returns pos. in array for stock data	 */void PrintData(struct stock &);char * GetMonth(int i)#endif[/SOURCE]

Originality is dead.
you are missing a semicolon on this line:

char * GetMonth(int i)
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
Quote:Original post by Sr_Guapo
you are missing a semicolon on this line:

char * GetMonth(int i)


Heh..guess so. Don't know why that'd give me those errors though.

Originality is dead.
Quote:Original post by BringBackFuturama
Quote:Original post by Sr_Guapo
you are missing a semicolon on this line:

char * GetMonth(int i)


Heh..guess so. Don't know why that'd give me those errors though.


It thinks that it is the start of the function, rather than a prototype. The next lines it found were inside of some header file. You cannot create a namespace inside of a function, so it threw some errors.
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
Bingo.

To the OP: the reason the errors appear to original in some headers you don't yourself include (such as cstddef) is because your header (which was missing the semicolon) was included before you include <iostream>. Since #include is just a textual substitution, and iostream probably includes some files itself (such as cstddef, eventually) the first errors the compiler sees appear to be in files that are not yours. If you had switched the include order, you might have had a an easier time tracking the mistake down.

Is this a C course or a C++ course, by the way? Because your mixing styles from both languages... which is generally bad, because they are very different languages dispite the apparent level of syntactic similarity.
Quote:Original post by jpetrie
Bingo.

To the OP: the reason the errors appear to original in some headers you don't yourself include (such as cstddef) is because your header (which was missing the semicolon) was included before you include <iostream>. Since #include is just a textual substitution, and iostream probably includes some files itself (such as cstddef, eventually) the first errors the compiler sees appear to be in files that are not yours. If you had switched the include order, you might have had a an easier time tracking the mistake down.

Is this a C course or a C++ course, by the way? Because your mixing styles from both languages... which is generally bad, because they are very different languages dispite the apparent level of syntactic similarity.


Ya..I get that now. Thanks...I'll keep that in mind in the future.

And my class is C++, but it's from a teacher who really likes C, so he tries to teach with a lot of C using C++. I'm gonna have to learn strings on my own.

Originality is dead.
Quote:Original post by BringBackFuturama
And my class is C++, but it's from a teacher who really likes C, so he tries to teach with a lot of C using C++. I'm gonna have to learn strings on my own.


Get out ASAP (if it is indeed possible). Proper, modern C++ is a different language.

Anyway, I include library headers first and then my own headers; then if something like this happens, the errors at least get reported against my own code, even if it's the "wrong" file.
Quote:Original post by Zahlman
Quote:Original post by BringBackFuturama
And my class is C++, but it's from a teacher who really likes C, so he tries to teach with a lot of C using C++. I'm gonna have to learn strings on my own.


Get out ASAP (if it is indeed possible). Proper, modern C++ is a different language.

Anyway, I include library headers first and then my own headers; then if something like this happens, the errors at least get reported against my own code, even if it's the "wrong" file.


hell yah it is. I learned c before I learned c++ and everything you learn from c you pretty much had to throw out the window. the only thing you really carry over is datatypes. most of the algorithms are don't completely differently because of use of containers and overloaded operators, not to mention classes.

This topic is closed to new replies.

Advertisement