-
March 6th, 2009, 11:01 PM
#1
Unable to compile?
Hi all,
I am trying to use 2 files, abc.cpp and test.cpp, where test.cpp uses a "#include abc.cpp" to include abc.cpp. But there seem to be some problems and i can't seem to find out why. If someone could point me in the right direction would be great:
[CODE
//abc.cpp
]int solve(P & p) {
return 1; // this does nothing at the moment
}[/CODE]
Code:
//test.cpp
#include<iostream>
using namespace std;
class L {};
class H {};
class P {
int val;
public:
P(int Val) {
val = Val;
int i=max-min+1;
while(i/=2) lgn++;
}
void testing123(int g){
if(g<val) throw L();
if(g>val) throw H();
return;
}
};
int main()
{
for(int i=0; i<=1280; i++)
if(solve(P(0,1280,i)) != i) cout << "wrong answer\n";
system("pause");
return 0;
}
-
March 6th, 2009, 11:24 PM
#2
Re: Unable to compile?
You just need to include header file to it.
Thanks for your help.
-
March 7th, 2009, 05:06 AM
#3
Re: Unable to compile?
Actually i did try adding to the test.cpp file, but it doesn't seem to work and gives me a compilation error.
-
March 7th, 2009, 05:49 AM
#4
Re: Unable to compile?
just include the header file
Code:
//abc.hpp
#ifndef ABC_HPP
#define ABC_HPP
int solve(P & p);
#endif
//abc.cpp
#include "abc.hpp"
int solve(P & p) {
return 1; // this does nothing at the moment
}
max, min, and lgn are not declared in the scope of P(int Val).There is no matching constructor P(int, int, int). If you want to pass a P object by reference, you may want to define one.
Last edited by richard_tominez; March 7th, 2009 at 06:57 AM.
-
March 7th, 2009, 06:58 AM
#5
Re: Unable to compile?
The abc.hpp header file also requires a foward-declaration for class P.
abc.hpp:
Code:
#ifndef ABC_HPP
#define ABC_HPP
class P;
int solve(P & p);
#endif
The file abc.cpp would need to include the class declaration for P... this does not exist, but let's pretend it does.
abc.cpp:
Code:
#include "abc.hpp"
#include "P.hpp"
int solve(P& p)
{
...
}
Hopefully the OP will realize/understand the benefits of modular programming. If in the end he has an abc.cpp, a P.cpp, and a test.cpp, they will be compiled in the following manner:
Code:
g++ abc.cpp P.cpp test.cpp
Of course a Makefile would be nice to have, but it seems too advanced of a subject right now for the OP.
Last edited by dwhitney67; March 7th, 2009 at 07:18 AM.
-
March 7th, 2009, 07:14 AM
#6
Re: Unable to compile?
Oh yeah, sorry my false lol!
-
March 7th, 2009, 08:52 AM
#7
Re: Unable to compile?
Thanks for the reply all, but i am a bit confused. Actually, the given lab questions says i have 2 files, abc.cpp and test.cpp. So in abc.cpp will contain the solve() method and i don't think i am able to create another header file. So is there anyway to provide a link between these files without a .h file? Anyway, i made these changes and i am not sure if i have done it right or am i missing something because i get 2 errors:
`p' has incomplete type
forward declaration of `struct P'
Code:
//abc.cpp
#ifndef ABC_HPP
#define ABC_HPP
class P;
int solve(P & p);
#endif
class P;
int pastValues[1280];
int countIndex=0;
int solve(P p) {
return 0;
}
Code:
//test.cpp
#include<iostream>
using namespace std;
class L {};
class H {};
class P {
int val;
public:
P(int Val) {
val = Val;
}
void testing123(int g){
if(g<val) throw L();
if(g>val) throw H();
return;
}
};
int main()
{
for(int i=0; i<=1280; i++)
if(solve(P(0,1280,i)) != i) cout << "wrong answer\n";
system("pause");
return 0;
}
#include "poser.cpp"
int main()
{
for(int i=0; i<=1280; i++)
if(solve(P(0,1280,i)) != i) cout << "wrong answer\n";
system("pause");
return 0;
}
-
March 7th, 2009, 09:04 AM
#8
Re: Unable to compile?
Your abc.cpp is incomplete; it does NOT know what a P is. You need to provide it the class declaration.
Your reference material is poorly written; disregard it and think outside the box.
You have two choices.
1) Implement everything in one .cpp file, starting with the declaration of class P, followed by everything else; or
2) Modularize your application to have a P.hpp, a P.cpp, an abc.hpp, an abc.cpp, and possibly a Main.cpp. The latter module is optional; you can place main() in either P.cpp or abc.cpp, but I do not recommend this.
P.S. If your reference material is indicating that you #include "abc.cpp", then it is truly a piece of work not worthy of cleaning my buttocks.
-
March 7th, 2009, 09:21 AM
#9
Re: Unable to compile?
I definitely agree i should modularize my work, but unfortunately that's my lab requirement and i must only submit these 2 files. I wish i could put everything in one file too to save me the trouble. Really appreciate it if you could help me out.
Your abc.cpp is incomplete; it does NOT know what a P is. You need to provide it the class declaration.
So how do i provide a class declaration in abc.cpp?
-
March 7th, 2009, 09:23 AM
#10
Re: Unable to compile?
I definitely agree i should modularize my work, but unfortunately that's my lab requirement and i must only submit these 2 files. I wish i could put everything in one file too to save me the trouble. Really appreciate it if you could help me out.
Your abc.cpp is incomplete; it does NOT know what a P is. You need to provide it the class declaration.
So how do i provide a class declaration in abc.cpp?
-
March 7th, 2009, 09:39 AM
#11
Re: Unable to compile?
I can't believe I'm writing this... just #include <abc.cpp> right above the main() function in your test.cpp file.
Code:
...
class P
{
};
#include "abc.cpp"
int main()
{
...
}
Then compile your application like:
-
March 7th, 2009, 10:15 AM
#12
Re: Unable to compile?
Thanks! It works, i was arguing with my lecturer about this too. But he said it is for grading purposes. Can't fight him if i want to get any grades ...
-
March 7th, 2009, 10:27 AM
#13
Re: Unable to compile?
Any class that requires you to #include a cpp file (and isn't dealing with templates at the time) isn't worth taking.
-
March 7th, 2009, 12:44 PM
#14
Re: Unable to compile?
I know cause i argued with him a couple of other times too on how the concept is defeated by his lab submission requirements. Annoying cause i want to do it right too ... Thanks all!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|