-2

I have a global string variable in my cpp file, and am using it in a header file, and it is giving me the C2065 error, when the last time I had this project and created the header file it worked, I made no changes to either file and it is giving me errors for the global variable. It's also giving me the C3861 error while trying to use a global function in the header file declared in the cpp file, when it worked before as well. Everything was normal before I saved and closed my project, but now it's not working at all.

cpp file:

#include "stdafx.h"
#include "header.h"
#include <iostream>
#include <stdlib.h>
#include <string>
#include <windows.h>

using namespace std;

void story();       //--Function Declaration--
void tutorial();
void startGame();
void chooseClass();
void gameMenu();
void exitGame();    //--End Function Declaration--

string name;
string className;

header file:

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

void chooseClass(){
    className = "Warrior";

    gameMenu();
}

The header file uses the className string variables, and goes to the gameMenu function. The chooseClass function in the header file works without errors.

Errors:

header.h(32): error C2065: 'className' : undeclared identifier
header.h(35): error C3861: 'gameMenu': identifier not found
  • Can you add the code from your .h and .cpp files? At least the relevant pieces. – Omada Jan 02 '15 at 01:11
  • Probably you have a global variable in one of your headers. That means every compilation unit that includes that header would have the same global variable. You probably want one .cpp file with the global variable and export that variable to other compilation units by declaring the variable with 'extern' in the header. – hetepeperfan Jan 02 '15 at 01:17
  • I have no global variables in the header file, only the cpp file – Dustin Yule Jan 02 '15 at 01:21
  • 2
    Some of your definitions are in reverse of what they should be, i.e. the body of a function should be in the cpp file and not the header file (the header file would only contain the function definition, not the full function body). Class variables should be defined in the header, and **void chooseClass()** should be **void ::chooseClass()**. The code where **gameMenu** is referenced in header.h is also missing making that error harder to determine why it is occurring. – localhost Jan 02 '15 at 01:30
  • the chooseClass function can't compile due missing semicolon and `className` is not declared. – hetepeperfan Jan 02 '15 at 01:37
  • So if I want to separate functions it should be done in other cpp files? I haven't done much work with header files and multiple cpp files, but I heard in larger programs to separate functions for faster build times and less clutter. – Dustin Yule Jan 02 '15 at 01:37
  • @DustinYule That depends, it is recommended you would use the same cpp file if the functions contained within it relate to the same overall class. Another class would have another h and cpp file. For example, void chooseClass(); would be in header.h, while the full code for chooseClass(); would be in header.cpp (assuming the file name based on the header file name). I would also strongly recommend you add an ifndef specific to your header into your header file such as shown here: http://stackoverflow.com/questions/3246803/why-use-ifndef-class-h-and-define-class-h-in-h-file-but-not-in-cpp – localhost Jan 02 '15 at 01:43

1 Answers1

0

in your header write:

void chooseClass(const std::string& name);

this declares the function.

then in your cpp file you would implement the function.

// the global variable
std::string className;

//set the global variable to a value.
chooseClass(const std::string& name)
{
    className = name;
}

Then in another .cpp file you would use the set choose class function like chooseClass("warrior");

Then as a sidenote: you might want to use enumerations for your game classes.

hetepeperfan
  • 4,292
  • 1
  • 29
  • 47