簡體   English   中英

Visual Studio C ++中的鏈接器問題

[英]Linker issue in Visual Studio C++

我正在嘗試在VS 2017中使用C ++(空項目模板),但是在添加1個簡單類時立即遇到了鏈接器問題,所以我想我缺少了一些重要的東西...

我的項目如下所示:

項目結構

test.h

#include <iostream>

class test
{
public:
    test();
    ~test();

    std::string getInfo();
};

test.cpp

#include "test.h"

test::test() {}
test::~test() {}

std::string getInfo() {
    return "test";
}

main.cpp

#include <iostream>
#include <string>
#include "test.h"

int main(int argc, char **argv) {
    test t;
    std::cout << "output: " << t.getInfo() << std::endl;

    return 0;
}

我收到的鏈接器錯誤是臭名昭著的LNK2019:

LNK2019 unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl test::getInfo(void)" (?getInfo@test@@QEAA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function main

有什么想法我在這里做錯了嗎? 謝謝!

在文件test.cpp您需要正確指定成員函數的范圍:

std::string test::getInfo() {
    return "test";
}

注意test::getInfo()之前

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM