beginner c++ programmer compile errors

This is a discussion on beginner c++ programmer compile errors within the C++ Programming forums, part of the General Programming Boards category; Hi, I am very new to C++ and am trying to get a very small example to work on building ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    5

    beginner c++ programmer compile errors

    Hi, I am very new to C++ and am trying to get a very small example to work on building a very simple class. The code is as below:
    Code:
    #include <stdio.h>
    
    class MyClass{
    public:   
       void setNum(int num)
       {
          number = num;
       }
       
       int getNum()
       {
          return number; 
       } 
    private:
       int number;
    };
    
    int main()
    {
    	MyClass tmp;
    	
    	tmp.setNum(3);
    		
    	printf("Your number is %d\n", tmp.getNum());
    	return 0;		
    }
    I try to compile this code using the following command:

    gcc -o tmp tmp.cpp
    and I get the following compiler error messages:

    Undefined first referenced
    symbol in file
    __gxx_personality_v0 /var/tmp/cc2h4zeU.o
    ld: fatal: Symbol referencing errors. No output written to tmp
    collect2: ld returned 1 exit status
    However if I compile the above code without the bold line (i.e the following):
    Code:
    #include <stdio.h>
    
    class MyClass{
    public:   
       void setNum(int num)
       {
          number = num;
       }
       
       int getNum()
       {
          return number; 
       } 
    private:
       int number;
    };
    
    int main()
    {
    	MyClass tmp;	
    	
    		
    	//printf("Your number is %d\n", tmp.getNum());
    	return 0;		
    }
    It compiles fine. Its seems to give the above error as soon as I try to use any instance of MyClass.

    This may be a trivial problem but I would be grateful for any help.

    Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    #include <cstdio>

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Use g++ for compiling C++ files, not gcc.


    >> #include <cstdio>
    #include <stdio.h> should work fine, although for C++ I'd use <iostream> and cout. Besides, this is a linking error, not a compiling error.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    5
    I have already tried that and I get the same error, actually my current version of the code does have the cstdio not the stdio, as I said it compiles when I don't do anything to the instance of MyClass but as soon as I try to do anything, I get the error.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    5
    Thanks Daved, I used g++ to compile it and it worked fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner programmer
    By Kool4School in forum C Programming
    Replies: 16
    Last Post: 11-24-2008, 12:16 PM
  2. DirectX9 compile errors
    By maxthecat in forum Windows Programming
    Replies: 5
    Last Post: 01-01-2006, 09:33 PM
  3. logical errors when i compile.
    By findme in forum C++ Programming
    Replies: 4
    Last Post: 11-17-2005, 03:55 PM
  4. [C] - String Manipulation {Beginner Programmer}
    By INFERNO2K in forum C Programming
    Replies: 14
    Last Post: 05-21-2005, 11:34 AM
  5. Dev-c++ 4.0 compile errors
    By DigitalNOISE in forum C++ Programming
    Replies: 9
    Last Post: 07-12-2002, 04:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21