0

enter code heremy code is not compiling, I am using VSCode and these files, but I do not see anything wrong, the compiler literally says this

/home/cristian/Documents/PersonalCode/Cpp/Houses/main.cpp:16: undefined reference to `House::setNumStories(int)'

But, it does not make sense to me, everything seems to be right because I am calling the respective variables.

Is it possible that because I am using VSCode in ubuntu and NOT visual studio that has some kind of relationship with my problem ?

Sorry if the question is too basic, I am just getting started with C++

Thanks in advance.

Here are the files :

main.cpp

#include <iostream>
#include <string>
#include "House.h"

using namespace std;



//int printHouseData(House Info);

int main()
{
    House myHouse;
    House yourHouse;

    myHouse.setNumStories(2);
    myHouse.setNumWindows(6);
    myHouse.setColor("red");

    yourHouse.setNumStories(3);
    yourHouse.setNumWindows(10);
    yourHouse.setColor("blue");

    /*printHouseData(myHouse);

    printHouseData(yourHouse);*/

    cout << "All is good !"<< endl;

    return 0; 
}


/*int printHouseData(House Info)
{
    cout << "My house is "<<Info.getColor() << " and has "
    << Info.getNumStories() << " stories and "
    << Info.getNumWindows() << " windows." << endl;

    return 0; 
}*/

House.h

#include <string> 
using namespace std;

class House {
    public:
       void setNumStories(int numStories);

       void setNumWindows(int numWindows);

       void setColor(string color);
       

       int getNumStories() const;       

       int getNumWindows() const;       

       string getColor() const;               

    private:
       int numStories;
       int numWindows;
       string color; 
};

House.cpp

#include "House.h"
using namespace std;

void House::setNumStories(int numStories)
{
    this -> numStories = numStories;
}

void House::setNumWindows(int numWindows)
{
    this -> numWindows = numWindows;
}

void House::setColor(string color)
{
    this -> color = color;
}

int House::getNumStories() const
{
    return numStories;
}

int House::getNumWindows() const
{
    return numWindows;
}

string House::getColor() const 
{
    return color;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • You have to compile all cpp files and link them. Unrelated: 1. You should use include guards. 2. Don't use `using namespace std; – Thomas Sablik Mar 06 '21 at 02:42
  • 1
    ***Is it possible that because I am using VSCode in ubuntu and NOT visual studio that has some kind of relationship with my problem ?*** Yes. By default VSCode is setup to build only the active file. Not all files in the folder. The instructions here explain how to build all .cpp files in the folder: [https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson) – drescherjm Mar 06 '21 at 03:05
  • The linux version of this documentation on how to modify `tasks.json` to build more than 1 source file is here: [https://code.visualstudio.com/docs/cpp/config-linux#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-linux#_modifying-tasksjson) – drescherjm Mar 06 '21 at 03:08
  • To clarify your question and to make it relevant to VS Code, you will need to provide your task.json that shows how you are compiling your application using VS Code. – Gino Mempin Mar 06 '21 at 03:12
  • everything in `House.cpp` can be written in `House.h` and thus inlined by the compiler – rioV8 Mar 06 '21 at 07:59
  • if you have more than 1 source file use a build tool: Make CMake, ... – rioV8 Mar 06 '21 at 07:59

0 Answers0