C++ 构造函数未显示输出



我有以下 3 个文件,main.cpp 然后是一个类定义,包括该类的头文件:

主.cpp:

#include <iostream>
#include "data_vars_class.hpp"
int main () {

DataVars dataVars();
return 0; 

}

data_vars_class.hpp:

#include <string>
#include <vector>
class DataVars
{
private:
std::vector<std::string> csv_card_names;
public:

DataVars();
void getCSVData();
}; 

data_vars_class.cpp:

#include <iostream>
#include <vector>
#include <string>
#include "data_vars_class.hpp"
DataVars::DataVars()
{
std::cout << "constructor?";

getCSVData();
}
void DataVars::getCSVData() 
{
std::cout << "Getting csv data!";
}

问题是当我构建和执行代码时,我只得到一个空终端。我知道 data_vars_class.hpp 和 data_vars_class.cpp 都包含在构建中,这是我在 Geany 中的构建命令:

g++ main.cpp data_vars_class.cpp -o a.out

为什么我在终端中看不到 cout 输出,就像在构造函数中我不应该在终端中看到"构造函数吗?

谢谢

通过构建二进制文件来创建运行代码,您必须在构建后在终端中编写 ./a.out。

g++ main.cpp data_vars_class.cpp -o a.out
./a.out

最新更新