Advertisement

multiple .cpp files for newbies

Started by September 12, 2000 09:59 AM
22 comments, last by Newbie-O-Rama 24 years, 3 months ago
Hi! Yes, I''m still at the text-only game phase, and up until now all my code has been in one big-*ss .cpp file. My new project is going to be broken up into several .cpp files to make it look a little more professional. So here''s my problem, I keep getting an ''unexpected end of file'' error in my extra .cpp files. Here is a stripped down version of what I have: main(blah, blah, blah..... { //stuff here, blah, blah, blah.... otherFunction(); // calls up separate .cpp file } //end of main // here is other file: void otherFunction(): { cout << "Funcion stuff" << endl; } //this is the line the eof error points to What am I doing wrong? Thank you for looking at this.
quote:
void otherFunction():


There shouldn''t be an : at the end of the line there...
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Advertisement
Sorry, that was a typo on my post.
My code does not have a ":" following void otherFunction()
Any other ideas?
thanks
Stuff like this usually happens when you have forgotten a } character somewhere. Use search and replace to find out how many { characters you have, and then find out how many } characters you have in your source file(s). (Don''t save the file after you''ve searched and replace ) If the numbers you get don''t match, you''ve forgotten a { or } character somewhere.
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Thanks for the help, but both ''{'' and ''}'' turned up 4
times.....ugh, it should have been 6 each if it counted
the main function.
Could it have something to do with a header file i used
to declare my global variables and all my extra functions?
I thought it would look nicer that way instead of having a
huge list of functions in my main .cpp file.
I''ve never split a project into multiple .cpp files, so can''t help ya there. IT worked when I stuck functions in a .h, so you might try that.

One thing to look for: I''m pretty sure you get an unexpected end of file if you forget a ";", be sure to check you have all semi colons where appropriate
Peon
Advertisement
1) Main shouldn''t have more than 2 args (well.. three but its not ansi standard)

2) int main(), not main(), main must return int

That probably wont fix your problem, so:

Do you get any other errors? What compiler are you using?

Make sure that you turn the compiler to its highest warning level (level 3 for VC++ is high enough, otherwise its just bitching)

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
do you have
    #include "otherFunction.cpp"    

?

if so, don''t do this. create a header file, called otherFunction.h, then put the function definition in the header. void otherFunction();

then in the main source file, include otherFunction.h instead of otherFunction.cpp


a2k
------------------General Equation, this is Private Function reporting for duty, sir!a2k
OK, bless you all for helping me with this!!
I got rid of the whole project and made a very short new one to let me figure out what is wrong. I have a main function, which calls up 2 other functions. I put the other two in there own header file, and here is the entire project:

//this is what my main cpp file contains:
#include "stdafx.h"
#include "myVariables.h"

int main(int argc, char* argv[])
{
intro();
town();
return 0;
} //end of main

//this is what the "myVariables.h" contains:
void town();
void intro();

// this is what the header town.h contains:
void town();
{
cout << "Town" << endl;
}

//this is what the header intro.h contains:
void intro();
{
cout << "Intro" << endl;
}


===========
Those are the 4 files I have written, and now I get 3 errors:
1)error LNK2001: unresolved external symbol "void __cdecl town(void)" (?town@@YAXXZ)
2)same thing as one, but for intro instead of town
3) fatal error LNK1120: 2 unresolved externals

Any advice? Want me to go jump in a lake? The books I have are no help, so I would really appreciate any help I can get.
Thanks (again)
Ok here it goes...my very short tutorial...

create a project that contains the files that you needed...Do not put actual code in the .h files. The .h file only declare the functions or classes instead create two more .cpp files and title them the same as the two .h files you have. To complete this you need to declare in the .cpp files that you have declared the function in a .h file so at the begining of the new .cpp files just add "#include "File.h". Ok now if your reading this and going duh then here is an example

//Main file

#include "Functions.h"
#include "Variables.h"

#include
// or what ever it is spelled like! I don'nt
// use dos mode anymore

int main()
{
// Bla bla bla
int Var;
DisplayText("Hello World");
Var = GetVar();
return 1;
};

//End of file

// Functions.h

void DisplayText(char text[]);

// Functions.cpp

void DisplayText(char text[]);
{
cout << text << endl;
};

// Variables.h

int GetVariable();

// Variables.cpp

int GetVariable();
{
return 5;
};

// End of file

If this does not help just go ahead and jump in that lake or give me an email and I will try to help you out with some more specific questions


"If I wanted to hear the pitter patter of little feet I would put shoes on my cat!"- One Great Programmer (Garret Foster)

Edited by - Ecko on September 12, 2000 11:22:31 PM
-----------------------------------------------------------"People who usualy use the word pedantic usualy are pedantic!"-me

This topic is closed to new replies.

Advertisement