0

The header:

#ifndef DECLARATIONS_H_
#define DECLARATIONS_H_

#endif /*DECLARATIONS_H_*/

#include <iostream>
#include <cctype>
using namespace std;

extern int pointer;

Main cpp:

#include "Declarations.h"
int main() {

    string input = "something something";
    int pointer = input.length();
}

And then when I use it in another .cpp that includes the header at the very top and uses "pointer", it gives an undefined reference pointer.

I don't think it is a linking problem either because when I use functions from the C libraries that I included in the header file in the cpp files it works.

Basilevs
  • 22,440
  • 15
  • 57
  • 102
Yao Li
  • 9
  • 1
  • _"I don't think it is a linking problem ..."_ Wrong. It's definitly a linker problem, you must have a definition for that variable in one of your translation units: `int pointer;`. The definition in `main()` shadows the declaration from the `extern` statement. – πάντα ῥεῖ Feb 11 '16 at 14:27
  • If it is a linker problem, how do I fix it? – Yao Li Feb 11 '16 at 14:30
  • I mentioned what you need to do: Provide a definition for that variable. – πάντα ῥεῖ Feb 11 '16 at 14:31
  • But my int pointer = input.length() is a definition of the variable, is it not? What do you mean it "shadows" the declaration? – Yao Li Feb 11 '16 at 14:33
  • As also mentioned it's not the definition of the variable from the `extern` statement, but a local variable inside the `main()` function. – πάντα ῥεῖ Feb 11 '16 at 14:46

0 Answers0