Thread: class using other files

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    72

    class using other files

    hi im trying to figure out how to make this work

    i have main.cpp, asd2.cpp and asd.h

    asd.h has the class inside

    here is the code

    Code:
    class a {
          int x;
          public:
                 int getX();
                 void setX(int);
                 };
    asd2.cpp has some functions from class a

    Code:
    #include <iostream>
    #include "asd.h"
    
    void a::setX(int b){
         x = b;
         }
    int a::getX(){
        return x;
    }
    and finally here is the main

    Code:
    #include <iostream>
    #include "asd.h"
    
    using namespace std;
    
    
    int main(void){
       
        a b;
        b.setX(109);
       cout<<b.getX();
       cin.get();
        return 0;
    }
    im wondering why it doesnt work, it says

    Code:
      [Linker error] undefined reference to `a::setX(int)' 
      [Linker error] undefined reference to `a::getX()' 
      ld returned 1 exit status
    i think it has something to do with the "include"

    does anyone know how to fix this problem? thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps you did not compile asd2.cpp and link to the object file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    Quote Originally Posted by laserlight View Post
    Perhaps you did not compile asd2.cpp and link to the object file.
    when I compile asd2.cpp

    i get this error

    Code:
      [Linker error] undefined reference to `WinMain@16' 
      ld returned 1 exit status

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Compile separately and then link, or compile both of them and link simultaneously. What compiler are you using? I would guess g++, and thus:
    Code:
    g++ main.cpp asd2.cpp
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    Quote Originally Posted by laserlight View Post
    Compile separately and then link, or compile both of them and link simultaneously. What compiler are you using? I would guess g++, and thus:
    Code:
    g++ main.cpp asd2.cpp
    ok thanks actually i have Dev C++

    i created a new project and it did the linking by itself, i didnt know that i had to create a new project first :/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Whats the best way to control a global class?
    By parad0x13 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2009, 05:17 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  4. problem with sending files to a class method..
    By utoots in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2003, 01:38 AM
  5. Replies: 0
    Last Post: 11-16-2001, 12:28 PM