我似乎无法让 msvc 链接器在 vscode 上正常工作
I can't seem to get the msvc linker to work properly on vscode
我有一个非常简单的程序,由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);
:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- vscode g++链路故障:体系结构x86_64的未定义符号
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 当使用带有VS2019或VSCode的虚幻引擎4.24.2时,我如何修复这些错误的Intellisense错误
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- 我似乎无法让 msvc 链接器在 vscode 上正常工作
- Vscode 自动格式化代码在运行几次后停止工作
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- C++程序已停止工作-求解常微分方程