0

This is my first time having separate files and first time writing a header file, however I keep getting the same error I can't fix. Here are the files:

//main.cpp    
#include <iostream>
#include "Bike.h"

/*
class Bike{
public:
     int tyreDiameter;
     int getTyreDi(){
         return tyreDiameter;
    }
}; */

int main(){
    Bike b;
    b.tyreDiameter = 50;
    std::cout << b.getTyreDi();

while (1){
    continue;
}

return 0;
}

//Bike.cpp
class Bike{
    public:
        int tyreDiameter;
        int getTyreDi(void){
            return tyreDiameter;
        }
};

//Bike.h
#ifndef BIKE_H
#define BIKE_H

class Bike{
    public:
        int tyreDiameter;
        int getTyreDi(void);
};

#endif

Now if I have only one file and use the class that is commented out in main.cpp everything works fine. But as soon as I try to separate the Bike class into another cpp file I get this error:

Error 1   error LNK2019: unresolved external symbol "public: int
__thiscall Bike::getTyreDi(void)" (?getTyreDi@Bike@@QAEHXZ) 

Error 2   error LNK1120: 1 unresolved externals

I am using Microsoft Visual Studio 2013. Any help would be much appreciated

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Mick
  • 89
  • 9

1 Answers1

0

Why are you defining class Bike twice? in the cpp and in the h, the correct way would be this: header

//Bike.h
#ifndef BIKE_H
#define BIKE_H

class Bike{
    public:
        int tyreDiameter;
        int getTyreDi(void);
};

#endif

cpp

//Bike.cpp
#include "Bike.h"
int Bike::getTyreDi(void)
{
  //implementation like return tyreDiameter;
}
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
  • Thanks for the quick reply. So from what you are saying would I be right to assume that the header file describes what the class will look like syntactically and the cpp file just defines the functions of that class? – Mick Aug 02 '15 at 17:08
  • @Mick Correct. The header file describes what the class is supposed to do and the cpp file describes how to do it. – Hatted Rooster Aug 02 '15 at 17:09
  • Thanks a lot Jamey. Just one more thing please; what you said works perfectly in MSVS2013 however I like to use Notepad++ and compile using the MSVS2013 command line compiler. When I try to do the command 'cl /EHsc main.cpp' (without quotes) it gives me the same error as above. Any ideas please? – Mick Aug 02 '15 at 17:15
  • I worked it out. I just needed to put 'cl /EHsc main.cpp Bike.cpp'. Thanks again. – Mick Aug 02 '15 at 17:16