Problems with Compiling?

Hi all,

I am having problems trying to compile 2 files, a.cpp and test.cpp. So my problem is that when compiling a.cpp, it states that the P class in test.cpp is not declared in a.cpp. But i used "#include" in the test.cpp, so since it only contains a function, it shouldn't be a problem? And i am only allowed to edit a.cpp as stated as one of my lab requirements. Anyway, below i added the codes of my programs.

My error messages are:
test.cpp invalid initialization of non-const reference of type 'P&' from a temporary of type 'P'.
a.cpp in passing argument 1 of `int testing(P&)'


So basically the code for a.cpp does nothing at the moment:

1
2
3
4
int testing(P &p){
    
   return 0;   
}


I have a test.cpp file with some classes defined and a int main() at the end.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class P {
private:
int aa, bb, cc
public:
P(int a, int b, int c) {
aa= a;
bb= b;
cc = c;

void guess(int g){
if(++cnt > lgn) return;
if(g<cc) throw L();
if(g>cc) throw H();
return;
}
};

#include "a.cpp"

int main() {
for(int i=0; i<=111; i++)
if(solve(P(0,111,i)) != i) cout << "success\n";
}
Last edited on
Compilers compile each .cpp file on their own. It isn't until the linking process that multiple files are merged together. Therefore every kind of class or variable name you use in a .cpp file must be declared in that .cpp file (or #included in that .cpp file)

This is why you use header files.

Class definitions usually go in header files so that they can get included into multiple .cpp files. The implementations of those classes then usually go in a seperate .cpp file.

Also -- since .cpp files are compiled, you typically do not #include them, as this will likely cause linker errors due to the same implementation existing multiple times.

example:

in "header.h":
1
2
3
4
class P
{
 // .. stuff here
};


in "a.cpp":
1
2
3
4
5
6
#include "header.h"

int testing(P& p)
{
  return 0;
}


in "test.cpp":
1
2
3
4
5
6
7
#include "header.h"

int main()
{
  // ... stuff here
  return 0;
}
Hi Disch,

I agree with using the header.h file, but i am given 2 files. I can't modify test.cpp except for a.cpp. And i also can't add in new files. So my hands are kind of tied here. Even though, i think it is wrong but i have no choice,
but i am given 2 files


Right. Each of these files need to be compiled seperately. test.cpp should know nothing about a.cpp.

It isn't until the linking process that the compiled object files are combined to form the final executable. But linking is an entirely different step in the process.

What compiler/IDE are you using?

And i also can't add in new files. So my hands are kind of tied here. Even though, i think it is wrong but i have no choice


This doesn't really make much sense. What do you mean by "add in new files"? All C++ environments worth using have a linker, so you can always build a project with multiple source files.
Last edited on
What i meant by i can't add in new files is that i can't have an additional header.h file because i am not allowed to for my lab submission. So i am using a Unix environment and i usually compile my files like this:

CC a.cpp test.cpp
You've gotta be misinterpretting your teacher's directions. He probably means you can't have additional source files. If he really is expecting you to have multiple source files and no header files, then I don't know what to tell you >_>.

I say, send him an e-mail or something to clarify these assignment guidelines.

If it comes back that you're right and you're not allowed to have a header file (which, again, I feel is extremely unlikely), then the only way to get around it would be to #include one of the .cpp files like you're doing... but don't compile a.cpp on its own (it will be compiled as part of test.cpp because it's #included). But again -- this really isn't something you should do -- you've gotta be misunderstanding the assignment.
Well, as written on my lab sheet, it states that i only need to submit one file (a.cpp) for submission. So highly unlikely i am wrong i guess? I did try to #include the .cpp file, so i guess i should compile like:

CC a.cpp test.cpp ---> right?
Err.... well if you can only submit one file, then just put everything in one file and don't #include anything. (If the guidelines say one file, why are you trying to make two?)

edit:

also... if you are #including "a.cpp", then you do not compile a.cpp because it becomes part of test.cpp. But again I have to stress that this isn't what you should be doing.
Last edited on
Well, the test.cpp file will be used by my lecturer to grade and see if i manage to do the job as required, which is to amend a.cpp. Otherwise, i won't want to go through so much trouble to be honest.
*scratches head*

Well I don't understand what's going on here. Either you and I are having communication troubles or you and your teacher are. I don't really think I can help you with this problem because I don't really understand your restrictions, or why you need multiple files, or why you can't have a header file. None of this is making any sense to me.

All I can say is get in touch with your teacher and figure out what format your code is supposed to be in.
His teacher sounds like an idiot... One would clearly think his explained this wrong.
If you look carefully at test.cpp you will see that it already #include a.cpp after the declaration of class P
So he should NOT be doing a seperate compilation of a.cpp (otherwise if he did manage to compile it, he would end up with redifinition errors by the linker)
he should only be compiling test.cpp
I saw that (hence my post 2 posts before this one). But he really shouldn't be encouraged to write his code this way.
I know i shouldn't be writing this way and i disagreed with him many times. But that's the lab requirement, and he's grading it.
Well do it whichever way is required -- but I really think you're misunderstanding the requirements. For one thing, "only need to submit one file" does not mean "can only submit one file". Plus if you have test.cpp and a.cpp, you're already submitting more than 1 file, so I don't see why you can't just submit a 3rd file for a header. And I still don't see why you need multiple files anyway.

But yeah -- do it whatever way is required. Though make sure you're actually doing what is required. I find your interpretation of the requirements highly unlikely.
Disch:

I think you misunderstood, yes there are 2 files, a.cpp and test.cpp. I will only be submitting a.cpp, which is what i am only required to edit. The test.cpp is provided by the lecturer to help us test our a.cpp code, to see if we are generating the correct output.
o_O

So test.cpp came with the #include "a.cpp" bit? Ugh -- your teacher is teaching you terrible, terrible practices.

Then the solution here is to just not compile a.cpp. Treat it as a header file because that's basically what it is (now that it's being #included). So to compile this program, the only source file you compile is test.cpp.

Sorry for all the confusion.
Yeah, i know my lecturer is crap. I told him it doesn't make sense too, but he said it's for grading purposes because he uses a script to grade our submissions.

Yeah, test.cpp have a "#include a.cpp" code in it. The problem is when i try to compile test.cpp, it gives me the errors like those in my first post. So that's why i am a bit puzzled.

Anyway, it's your not your fault you got confused and i know the whole structure of it sucks.
Topic archived. No new replies allowed.