简体   繁体   中英

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...

My project looks like this:

项目结构

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";
}

And 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 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:

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

Note the test:: before the getInfo()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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