0

Hey there I am trying to learn the basics of Classes and I defined a class and tried to set some initial values for the objects. Here are my files (I tried to include the class as a header):

My Header File

#include <string>
#ifndef ROBOTS_H
#define ROBOTS_H
using namespace std;

class Robots
{
public:
    Robots();   
    void setType(string);
    string getType();
    void setPrice(float);
    float getPrice();
    void printRobot();

private:
    string robotType;
    float robotPrice;
};
#endif

.cpp for the Header:

#include <iostream>
#include <string>
#include "Robots.h"
using namespace std;

Robots::Robots()
{
    robotPrice = 0;
    robotType = "NULL";

}

void Robots::setType(string type)
{
    robotType = type;       
}

void Robots::setPrice(float price)
{
    robotPrice = price;
}

string Robots::getType()
{
    return robotType;
}

float Robots::getPrice()
{
    return robotPrice;
}

void Robots::printRobot()
{
    cout << "Your Robot Information is as follows:" << endl;
    cout << "Model:" << robotType << endl;
    cout << "Price" << robotPrice << endl;
}

main file

#include <iostream>
#include <string>
#include "Robots.h"
using namespace std;

int main()
{
    Robots Robot1;
    cout << "Initial Properties of the Robot:" << endl;
    Robot1.printRobot();
    cout << " " << endl;
}

When I run the main I get these errors:

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "public: __thiscall Robots::Robots(void)" (??0Robots@@QAE@XZ) referenced in function _main testRobots C:\Users\kucar\source\repos\testRobots\testRobots.obj 1

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "public: void __thiscall Robots::printRobot(void)" (?printRobot@Robots@@QAEXXZ) referenced in function _main testRobots C:\Users\kucar\source\repos\testRobots\testRobots.obj 1

Severity Code Description Project File Line Suppression State Error LNK1120 2 unresolved externals testRobots C:\Users\kucar\source\repos\testRobots\Debug\testRobots.exe 1

Can someone tell me what I am doing wrong?

kucar
  • 245
  • 3
  • 13
  • Are you compiling all your .cpp files? – cigien May 01 '20 at 22:14
  • On Linux, after I merged your 3 files into 1 file (and trimmed the 'noise'), the code compiles and runs. Show us your compile command. – 2785528 May 01 '20 at 23:16
  • Before posting here, **please** do a basic search for the error message you're getting. *LNK2019 unresolved external symbol* has been asked about (and answered) many,many, many times before. The **very first thing** you should do is a basic search. – Ken White May 01 '20 at 23:55
  • I am a starter in the language and in the beginning of the question I stated that I am kind of new to concept classes. I did my search before posting the question, looked up for some possible reasons but since I am lacking knowledge and experience I could not get it working. That is why I posted my question as a seperate one. I was posted every line of my code because I thought maybe there is something wrong with my syntax. I had a bit of hard time reading and understanding those troubleshooting from other posts. – kucar May 02 '20 at 02:09

0 Answers0