我似乎无法让 msvc 链接器在 vscode 上正常工作

I can't seem to get the msvc linker to work properly on vscode

本文关键字:vscode 常工作 工作 链接 msvc      更新时间:2023-10-16

我有一个非常简单的程序,由2个文件组成。 main.cpp具有主要功能:

[main.cpp]
#include <iostream>
#include <string>
#include "calculator.h"
using namespace std;
int main() {
Calculator calc;
do {
string op, left, right;
float out;
cout << endl << "insert an operator and two numbers: ";
cin >> op;
if (calc.isOperator(op)) {
cin >> left;
cin >> right;
out = calc.doOp(op, left, right);
cout << endl << "result: " << endl;
}
else
cout << endl << "invalid operator" << endl;
} while(true);
}

calculator.cpp 具有 Calculator 类,calculator.h 具有该类及其中的每个函数或变量的声明。

[calculator.cpp]
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
using namespace std;
class Calculator {
vector<float>* mem_stack;
public:
Calculator() {
mem_stack = new vector<float>();
}
~Calculator() {
delete mem_stack;
}
float memPeek() {
return (*mem_stack).back();
}
float memPeek(const int& age) {
return (*mem_stack)[(*mem_stack).size() - age];
}
float doOp(const string& op, string& left, string& right) {
float a, b;
if (left[0] == 'r') {
left = left.substr(1, left.size() - 1);
a = memPeek(stoi(left));
}
else
a = stoi(left);
if (right[0] == 'r') {
right = right.substr(1, right.size() - 1);
b = memPeek(stoi(right));
}
else
b = stoi(right);
float out;
if (op == "+")
out = a + b;
else if (op == "-")
out = a - b;
else if (op == "*")
out = a * b;
else if (op == "/")
out = a / b;
(*mem_stack).push_back(out);
return memPeek();
}
bool isOperator(const string& op) {
bool out;
out = op == "+" && op == "-" && op == "*" && op == "/";
return out;
}
};
[calculator.h]
#pragma once
#include <vector>
#include <string>
class Calculator {
private:
std::vector<float>* mem_stack;
public:
Calculator();
~Calculator();
float memPeek();
float memPeek(const int& age);
float doOp(const std::string& op, std::string& left, std::string& right);
bool isOperator(const std::string& op);
};

当我尝试编译程序时,我在主函数中出现未解决的链接错误。它们看起来都像这样:

main.obj : error LNK2019: unresolved external symbol

我从计算器中获取每个函数.cpp在main中调用,包括构造函数和析构函数 我已经查找了我能找到的所有内容,但我仍然收到这些错误。有人可以帮助我吗? 我还只是一个菜鸟。

欢迎来到 StackOverflow!

在此期间,您可能已经解决了这个问题,但您的问题非常简单:在Calculator.cpp文件中,您基本上是在重新声明另一个 Calculator 类,该类隐藏了原始类,最终没有为其定义任何函数(只有.h文件中的声明(。若要解决此问题,请改为在.cpp中使用ClassName::functionName()声明成员函数。

没有尝试编译,但这应该有效:

[calculator.cpp]
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
using namespace std;

Calculator::Calculator() {
mem_stack = new vector<float>();
}
Calculator::~Calculator() {
delete mem_stack;
}
float Calculator::memPeek() {
return (*mem_stack).back();
}
float Calculator::memPeek(const int& age) {
return (*mem_stack)[(*mem_stack).size() - age];
}
float Calculator::doOp(const string& op, string& left, string& right) {
float a, b;
if (left[0] == 'r') {
left = left.substr(1, left.size() - 1);
a = memPeek(stoi(left));
}
else
a = stoi(left);
if (right[0] == 'r') {
right = right.substr(1, right.size() - 1);
b = memPeek(stoi(right));
}
else
b = stoi(right);
float out;
if (op == "+")
out = a + b;
else if (op == "-")
out = a - b;
else if (op == "*")
out = a * b;
else if (op == "/")
out = a / b;
(*mem_stack).push_back(out);
return memPeek();
}
bool Calculator::isOperator(const string& op) {
bool out;
out = op == "+" && op == "-" && op == "*" && op == "/";
return out;
}

作为旁注,我不知道你的编程背景,但有一件事让我感到你的代码非常奇怪。也许你来自 Java 或 C#,在那里你总是必须初始化对象成员变量;在这方面,C++更像 C,因为它允许您按值而不是引用来保存对象。这意味着您不必有指向vector<float>的指针,也不必自己分配/释放它;让编译器为您完成工作并按值使用它。

因此,与其做std::vector<float>* mem_stack;,不如简单地做std::vector<float> mem_stack;;这使得像(*mem_stack).push_back(out);这样的操作(或替代mem_stack->push_back(out);,使用->顺从运算符(更干净mem_stack.push_back(out);