-1

I cannot compile my C++ program and I don't understand why. Here's a simple representation of what is throwing errors:

hello/hello.cpp

#include "hello.h"

namespace MyHelloNS {
        MyHelloClass::MyHelloClass() {
            MyHelloVAR1 = "hi";
            MyHelloVAR2 = "dog";
        }
}

hello/hello.h

#pragma once

#include <string>

using namespace std;

namespace MyHelloNS {
    extern string MyHelloVAR1;
    extern string MyHelloVAR2;

    class MyHelloClass;
}

class MyHelloNS::MyHelloClass {
public:
    MyHelloClass();
};

main.cpp

#include "hello/hello.h"

int main() {
    MyHelloNS::MyHelloClass hi1;
}

I get two kinds of errors: unresolved external symbol in hello.obj

What's wrong?

zzDone
  • 1
  • 2
  • 2
    Post the full errors... explaining **what** is "unresolved" or "already defined". – underscore_d Jul 01 '20 at 10:23
  • Did you already try a re-build instead of a simple compile? (I've seen cases before where "unresolved" cases could be solved like this) – Dominique Jul 01 '20 at 10:23
  • 1
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please [edit] your question to include a full and complete copy-paste of the error messages you get. – Some programmer dude Jul 01 '20 at 10:23
  • 1
    By the way, nowhere do you actually *define* the variables `MyHelloNS::MyHelloVAR1` and `MyHelloNS::MyHelloVAR2`. In the header file you only *declare* them. – Some programmer dude Jul 01 '20 at 10:24
  • Never "using namespace ..." in .h files: https://stackoverflow.com/a/5849668/3378179 – Caduchon Jul 01 '20 at 10:25
  • 1
    The screen shot of the errors doesn't match the description you made of the errors. There's no 'already defined' errors in the screen shot. – john Jul 01 '20 at 10:33
  • 1
    Visual Studio have a tab where you can get the errors *as text*. Please always copy-paste errors as text rather than images. Images can't be searched, it's not possibly to copy-paste parts of the "text" in them, and they don't work with screen-readers. – Some programmer dude Jul 01 '20 at 10:38

1 Answers1

2

Add this to main.cpp (or hello.cpp)

namespace MyHelloNS {
    string MyHelloVAR1;
    string MyHelloVAR2;
}

This question has nothing to do with namespaces, you just aren't following the correct procedure to define a global variable.

john
  • 85,011
  • 4
  • 57
  • 81
  • I don't get how I am not defining the variable in the header file? – zzDone Jul 01 '20 at 10:53
  • `extern` means that you are *declaring* the variable in the header file (which is correct). You can declare a variable as many times as you like, but you must *define* it only once. So declare in the header file and define in one cpp file. – john Jul 01 '20 at 11:03
  • Basically you need to understand the difference between declaration and definition. A declaration just says that a variable (or function or whatever) is defined somewhere else. The linkers job is to resolve all declarations to their definitions. If the linker finds zero definitions or multiple definitions for a given symbol it signals an error. (This is actually a simplification, look up the One Definition Rule if you're interested in further reading). – john Jul 01 '20 at 11:09