1

I have created a new C++ project on a separate computer to properly demonstrate this issue; These are the project files :

Main.cpp

#include "Test.cpp";

int main()
{
    Test test;
    cout << test.getColor();
    return 0;
}
Test.cpp

#include <iostream>

using namespace std;

class Test
{
private:
    string color;

public:
    Test()
    {
        color = "blue";
    }

    string getColor()
    {
        return color;
    }
};

Whenever I change this line to another value (ex: "red") :

color = "blue";

And then run the project, it says it builds and succeeds at building the changes in Test.cpp . When the console opens and displays the value, it still displays the old value of the variable, being in this example "blue". Only when I manually click "Build > Rebuild Solution" does the program do what I expect and print "red". What is going on here

I've tried updating Visual Studio, creating a new project, and looked all over for an answer to this question without any success.

  • 4
    `#include "Test.cpp";` Why? Never include a cpp file. In this case "Test.cpp" should be named "Test.h" and the include should be `#include "Test.h"` with no semicolon at the end. – drescherjm Nov 15 '22 at 13:32
  • 1
    It's hard to be sure but clearly you are not organising your code correctly. Source file (.cpp files) should not be included, they should be added to the project. Header files are what you include. You can also add header files to your project but notice that there are separate sections for source files and header files. In this case your test.cpp file is actually a header file, so rename it to test.h and change the include correspondingly. – john Nov 15 '22 at 13:53
  • @drescherjm I'm new to C++ and this did in fact fix my issue (I believe anyways). When do I use .cpp files or .h files? I thought classes needed to be in a .cpp file. What needs to be in a .cpp file instead? – Nicolas Cinq-Mars Nov 15 '22 at 14:36
  • Declarations go in headers while the implementation goes in the cpp file. I will look for a question that explains the build process. – drescherjm Nov 15 '22 at 14:37
  • Related: [https://stackoverflow.com/questions/9579930/separating-class-code-into-a-header-and-cpp-file](https://stackoverflow.com/questions/9579930/separating-class-code-into-a-header-and-cpp-file) – drescherjm Nov 15 '22 at 14:38
  • @drescherjm Ok I understand this now, but wouldn't it work to simply put all code inside a header file and never worry about .cpp files or is it bad practice? – Nicolas Cinq-Mars Nov 15 '22 at 14:48
  • I almost always create 1 header and 1 cpp per class. That is unless the class is very small or a helper class for some other class where its implementation only needs to be visible to the other class. – drescherjm Nov 15 '22 at 14:49
  • @drescherjm Alright thank you, and one final question, can you put more than one class in a single header file / cpp file combo? – Nicolas Cinq-Mars Nov 15 '22 at 14:58
  • You can do that. Here is a question that discusses why its better to not do that: [https://stackoverflow.com/questions/28160/multiple-classes-in-a-header-file-vs-a-single-header-file-per-class](https://stackoverflow.com/questions/28160/multiple-classes-in-a-header-file-vs-a-single-header-file-per-class) – drescherjm Nov 15 '22 at 14:59

0 Answers0