class using other files

This is a discussion on class using other files within the C++ Programming forums, part of the General Programming Boards category; hi im trying to figure out how to make this work i have main.cpp, asd2.cpp and asd.h asd.h has the ...

  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
    21,457
    Perhaps you did not compile asd2.cpp and link to the object file.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,457
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 04: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, 12:38 AM
  5. Replies: 0
    Last Post: 11-16-2001, 11:28 AM

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