简体   繁体   English

Visual Studio C ++中的链接器问题

[英]Linker issue in Visual Studio C++

I'm trying to get started with C++ in VS 2017 (empty project template), but immediately ran into linker problems when adding 1 simple class, so I guess I'm missing something important... 我正在尝试在VS 2017中使用C ++(空项目模板),但是在添加1个简单类时立即遇到了链接器问题,所以我想我缺少了一些重要的东西...

My project looks like this: 我的项目如下所示:

项目结构

test.h : test.h

#include <iostream>

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

    std::string getInfo();
};

test.cpp : test.cpp

#include "test.h"

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

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

And main.cpp : 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;
}

The linker error I get is the infamous LNK2019: 我收到的链接器错误是臭名昭著的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

Any ideas what I am doing wrong here? 有什么想法我在这里做错了吗? Thanks! 谢谢!

In file test.cpp you need to properly specify the scope of the member function: 在文件test.cpp您需要正确指定成员函数的范围:

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

Note the test:: before the getInfo() 注意test::getInfo()之前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM