CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Aug 2003
    Posts
    20

    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;
    }

  2. #2
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Unable to compile?

    You just need to include header file to it.
    Thanks for your help.

  3. #3
    Join Date
    Aug 2003
    Posts
    20

    Re: Unable to compile?

    Actually i did try adding
    Code:
    #include "abc.cpp"
    to the test.cpp file, but it doesn't seem to work and gives me a compilation error.

  4. #4
    Join Date
    Mar 2008
    Posts
    30

    Re: Unable to compile?

    just include the header file
    Code:
    #include "abc.hpp"
    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.

  5. #5
    Join Date
    Dec 2008
    Posts
    56

    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.

  6. #6
    Join Date
    Mar 2008
    Posts
    30

    Re: Unable to compile?

    Oh yeah, sorry my false lol!

  7. #7
    Join Date
    Aug 2003
    Posts
    20

    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;
    }

  8. #8
    Join Date
    Dec 2008
    Posts
    56

    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.

  9. #9
    Join Date
    Aug 2003
    Posts
    20

    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?

  10. #10
    Join Date
    Aug 2003
    Posts
    20

    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?

  11. #11
    Join Date
    Dec 2008
    Posts
    56

    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:
    Code:
    g++ test.cpp

  12. #12
    Join Date
    Aug 2003
    Posts
    20

    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 ...

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  14. #14
    Join Date
    Aug 2003
    Posts
    20

    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
  •  





Click Here to Expand Forum to Full Width

Featured