Announcement

Collapse
No announcement yet.

C-C++ problem

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    C-C++ problem

    Hello guys,
    I'm both new to linux and c++, and I have problems compiling and running my elementary c++ program in shell;

    Code:
    Main.cpp:(.text+0x1a2): undefined reference to `Second::Second(int)'
    Main.cpp:(.text+0x1b5): undefined reference to `Second::setNumber(int)'
    Main.cpp:(.text+0x1c0): undefined reference to `Second::printStuff()'
    I keep getting this error whatever I do...
    Here is the code:

    ---Second.h----
    Code:
    #include <iostream>
    
    using namespace std;
    
    class Second
    {
    public:
    	Second( int );
    	void printStuff();
    	void setNumber( int );
    private:
    	int number;
    };

    -----Second.cpp-----
    Code:
    #include <iostream>
    
    using namespace std;
    
    #include "Second.h"
    
    Second::Second(int a)
    {
    	setNumber( a );
    }
    
    void Second::printStuff()
    {
    	cout << "The number is: " << number << endl;
    }
    
    void Second::setNumber( int c )
    {
    	number = c;
    }
    -----Main.cpp-----
    Code:
    #include "Second.h"
    
    int main()
    {
    	int g = 6;
    
    	Second obj(g);
        obj.printStuff();
    	obj.setNumber(4);
    	obj.printStuff();
    	
    	return 0;
    }
    In shell I used "g++ Main.cpp" than I had the error above, instead of the file a.out, I think this occurs because it uses gcc instead of g++ but I dunno what to do, any bit of help is appreciated, thanks for your answers.

    #2
    Re: C-C++ problem

    In Second.h theres a extra(?) semicolon at the end. Could that be it? Havent got that much experience with C++, have only done C.

    edit: thats not it. I think you need to include both .cpp files on the commmandline to gcc; g++ Main.cpp Second.cpp -o second

    Comment


      #3
      Re: C-C++ problem

      Code:
      [quote=conholster ]
      In Second.h theres a extra(?) semicolon at the end. Could that be it? Havent got that much experience with C++, have only done C.
      [/quote]
      
      No, semicolon is put at the end of a class after brace, syntax is completely correct and I checked it with
      [code]
      g++ -c Main.cpp
      Plus when you type c++ Main.cpp, it compiles, and produces an executable.
      You can do the same thing with
      Code:
      g++ -c Main.cpp
      Here is when the object file Main.o is formed
      Code:
      g++ -o executable Main.o
      This is also the time when I get error, and if that worked I could use:
      Code:
      ./executable
      to run it, so I get the error at producing an executable file (may be called linking?)[/code]

      Comment


        #4
        Re: C-C++ problem

        Originally posted by conholster
        edit: thats not it. I think you need to include both .cpp files on the commmandline to gcc; g++ Main.cpp Second.cpp -o second
        Oh...it worked thanks!

        Comment


          #5
          Re: C-C++ problem

          No problem

          Comment

          Working...
          X