0

I am having some issues with C++ currently, and i've googled around and it says im doing it correct.

I got a Network.h file that contains:

extern std::string response;

Then I got a Network.cpp file that contains:

#include "Network.h"

std::string response;

response = "example"

Then I have a main.cpp file that contains:

#include <iostream>
#include "Network.h"

int main(int argc, char* argv[]) {
    std::cout << "Test Response (" << response << ")" << std::endl;
}

But it's giving me unresolved external symbol, and I don't know why since I have defined it in 1 cpp file, and externed it in the header file.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
ZeeStra
  • 9
  • 6
  • Please show the exact error message. It might look like gibberish to you, but it is actually descriptive and extremely important. Note that you seem to be missing the `#` part of `#include` preprocessor directive. There's also no `main` function -- are you posting snippets or is this your whole program?? Unrelated pet peeve: Please don't begin a paragraph with "so". – paddy Jan 26 '21 at 03:51
  • Please show the CMakeLists.txt you're using to build this. If you don't have one, write it, use cmake, and come back if you have further problems. – Kuba hasn't forgotten Monica Jan 26 '21 at 03:54
  • In case you would be adding more to the header file, it'll be eventually necessary to add the `#pragma once` line to the header file as well, but for what you're showing thus far, it's not necessary even if it's bad practice not to have that include guard there. I wouldn't bother with wrapping the entire file in `#ifndef SOMETHING / #define SOMETHING / #endif` - all sensible compilers understand `#pragma once`. – Kuba hasn't forgotten Monica Jan 26 '21 at 04:01
  • Error LNK2001 unresolved external symbol "class std::basic_string,class std::allocator > the_response" (?the_response@@3V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) – ZeeStra Jan 26 '21 at 04:04
  • Yes it already has # infront of includes, and yes it's snippets of my code AND also i do have #pragma once in my network.h file aswell. – ZeeStra Jan 26 '21 at 04:05

0 Answers0