c++ – Modifying code doesn’t change the outcome in Visual Studio


I have a simple project about testing a class template, where I want to implement a general array. It has main.cpp and MyArray.hpp files.

I noticed that when I modify MyArray.hpp, e.g. add a line of cout << endl;, the output in the command prompt window when I click “Local Windows Debugger” doesn’t change, it doesn’t have an empty line. It looks like the code change has no effect.

The main.cpp file has the following code:

#include "MyArray.hpp"

void test01()
{
    MyArray<int> arr1(5);
}

int main()
{
    test01();

    return 0;
}

The MyArray.hpp file has the following code:

#pragma once
#include <iostream>
using namespace std;

template<typename T>
class MyArray
{
private:
    T* pAddress;
    int m_Capacity;
    int m_Size;

public:
    MyArray(int capacity)
    {
        // cout << endl; // Adding this line doesn't change the output
        cout << "constructor with parameter" << endl;
        this->m_Capacity = capacity;
        this->m_Size = 0;
        this->pAddress = new T[this->m_Capacity];
    }
};

I tried to debug it. I created a brand new project with the same files and same code. Now modifying MyArray.hpp changes the print-out, which is weird.

I found something weird in the Build output. For the old project, the Build output after modifying MyArray.hpp is:

Build started...
1>------ Build started: Project: Tutorial_Template_Class, Configuration: Debug Win32 ------
1>MyArray.hpp
1>Tutorial_Template_Class.vcxproj -> E:\Desktop\CPP_Projects\Tutorial_Template_Class\Debug\Tutorial_Template_Class.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

But, for the new project, modifying MyArray.hpp does change the output, and the Build output after modifying MyArray.hpp is:

Build started...
1>------ Build started: Project: Tutorial_Template_Class_test, Configuration: Debug Win32 ------
1>main.cpp
1>Tutorial_Template_Class_test.vcxproj -> E:\Desktop\CPP_Projects\Tutorial_Template_Class_test\Debug\Tutorial_Template_Class_test.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

The one with unexpected behavior has MyArray.hpp in the Build output, while the normal one has main.cpp in the Build output. What is the problem here? Did I set anything wrong with Visual Studio for the project that doesn’t change after I modify MyArray.hpp?

Leave a Reply

Your email address will not be published. Required fields are marked *