首页 > 解决方案 > 我似乎无法让 msvc 链接器在 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

我从在 main 中调用的calculator.cpp 中为每个函数获取它们,包括构造函数和析构函数 有人可以帮助我吗?我还只是个菜鸟。

标签: c++visual-c++visual-studio-codecompilation

解决方案


欢迎来到 StackOverflow!

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

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

[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);


推荐阅读