【发布时间】:2020-07-14 17:00:09
【问题描述】:
我的代码与此类似,但问题完全相同:在 VSCode 中编译程序时,我在 Test1.cpp 和 Test2.cpp 中得到“对 `Test1::v 的未定义引用”。我究竟做错了什么?我对 c++ 有点陌生,所以我刚刚下载了一个扩展,它使我自动成为了一个 c++ 项目。当我使用 Ctrl + Shift + B 运行程序时,它给了我这个错误,但是当我使用 Code Runner 扩展名时,它没有检测到 .cpp 文件。
// Test1.h
#include <iostream>
#include <vector>
using namespace std;
#ifndef TEST1_H
#define TEST1_H
class Test1{
public:
Test1();
static vector<Test1> v;
int a;
};
#endif
//Test1.cpp
#include "Test1.h"
Test1::Test1(){
a = 2;
v.push_back(*this);
}
//Test2.h
#include <iostream>
#include <vector>
using namespace std;
#ifndef TEST2_H
#define TEST2_H
class Test2{
public:
Test2();
double var;
};
#endif
//Test2.cpp
#include "Test2.h"
#include "Test1.h"
Test2::Test2(){
var = 5;
Test1::v[0].a += var;
}
//main.cpp
#include <iostream>
#include "Test1.h"
#include "Test2.h"
using namespace std;
int main(int argc, char *argv[])
{
cout << "Hello world!" << endl;
}
【问题讨论】:
标签: c++ visual-studio-code include header-files