C++ 我能';似乎无法使msvc链接器在vscode上正常工作
我有一个非常简单的程序,它由两个文件组成。 main.cpp具有以下主要功能:C++ 我能';似乎无法使msvc链接器在vscode上正常工作,c++,visual-c++,visual-studio-code,compilation,C++,Visual C++,Visual Studio Code,Compilation,我有一个非常简单的程序,它由两个文件组成。 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;
[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);
}
我在main中调用calculator.cpp的每个函数都会得到它们,包括构造函数和析构函数
我已经查找了所有我能找到的关于这个的东西,但是我仍然得到那些错误。有人能帮我吗?
我还是个新手。欢迎来到StackOverflow 在此期间,您可能已经解决了这个问题,但您的问题非常简单:在
Calculator.cpp
文件中,您基本上是重新定义了另一个计算器类,该类与原始类有阴影,最终没有为其定义任何函数(只有.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;
}
[calculator.cpp]
#包括
#包括
#包括
#包括
使用名称空间std;
计算器::计算器(){
mem_stack=新向量();
}
计算器::~Calculator(){
删除内存栈;
}
浮点计算器::memPeek(){
return(*mem_stack).back();
}
浮点计算器::memPeek(常量整数和年龄){
return(*mem_stack)[(*mem_stack.size()-age];
}
浮点计算器::doOp(常量字符串和op、字符串和左、字符串和右){
浮子a、b;
如果(左[0]=='r'){
left=left.substr(1,left.size()-1);
a=memPeek(stoi(左));
}
其他的
a=stoi(左);
如果(右[0]=='r'){
right=right.substr(1,right.size()-1);
b=memPeek(stoi(右));
}
其他的
b=stoi(右);
浮出水面;
如果(op==“+”)
out=a+b;
否则如果(op==“-”)
out=a-b;
否则如果(op==“*”)
out=a*b;
否则如果(op==“/”)
out=a/b;
(*内存堆栈)。向后推(出);
返回memPeek();
}
布尔计算器::等运算符(常量字符串和运算){
发出嘘声;
out=op==“+”&&op==“-”&&op==“*”&&op==“/”;
返回;
}
顺便说一句,我不知道你的编程背景,但有一件事让我觉得你的代码很奇怪。也许您来自Java或C#,在那里您总是必须初始化对象成员变量;C++在这方面有点像C,因为它允许你按值保存对象,而不仅仅是引用。这意味着您不必拥有指向向量的指针,也不必自己分配/取消分配它;让编译器为您完成工作,并按值使用它
因此,不要做std::vector*mem_堆栈代码>,只需执行std::vector mem_堆栈代码>;这使得像(*mem\u stack)这样的操作成为可能
(或者使用->
遵从操作符的替代方法内存堆栈->向后推(出);
)更干净:内存堆栈。向后推(出)代码>我猜您在没有编写默认构造函数的情况下重载了计算器构造函数。我们能看到Calculator.h和Calculator.cpp吗?具体的错误也会很有帮助。它应该告诉您哪个函数链接失败。请记住张贴这样的细节!配置VisualStudio代码以使用VisualStudio构建工具似乎有点奇怪。为什么不使用Visual Studio?谢谢。我编辑了问题并添加了calculator.cpp和.h。
[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
[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;
}