我在 M1 Mac 上运行 VScode。 每当我尝试运行我的代码时,我总是收到此错误:
Undefined symbols for architecture arm64:
"Array::Array(int)", referenced from:
_main in Main-528956.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
对于 C++ 类来说,这是一个非常简单的项目,但这个错误超出了我的范围。 这是我必须使用的三个类:
数组.h:
#ifndef _ARRAY_H_
#define _ARRAY_H_
class Array
{
public:
Array();
Array(int);
private:
int size;
int *arr;
};
#endif
数组.cpp:
#include <iostream>
#include "Array.h"
using namespace std;
Array::Array()
{
size = 0;
arr = nullptr;
}
Array::Array(int s)
{
size = s;
arr = new int[size];
for (int i = 0; i < size; i++)
{
arr[i] = i;
}
cout << "overloaded constructor works" << endl;
delete[] arr;
}
主.cpp:
#include <iostream>
#include "Array.h"
using namespace std;
int main()
{
Array arr1(5), arr2(10);
return 0;
}
如果我需要添加任何其他信息,请告诉我。
将
/usr/bin/clang
更改为 /usr/bin/clang++
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang build active file",
"command": "/usr/bin/clang++",
"args": [
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}