0

I am having a problem with my program where in I declared the variable from a header file and execute it on a CPP file. I am getting an error which is the undefined reference to the variable.

here a sample code of my project:

CPP FILE

#include "function.h"
CClass::CClass() : m_Db(HOST,USER,PASSWORD,DATABASE)
{
   ...
}

HEADER FILE

#ifndef CONNECTION_H
#define CONNECTION_H

#include "crypt.h"

extern CCrypt *c_crypting;

#define HOST c_crypting->Decrypt_Host()
#define USER c_crypting->Decrypt_Username()
#define PASSWORD c_crypting->Decrypt_Password()
#define DATABASE c_crypting->Decrypt_Database()

#endif // DBCONNECTION_H

if i run this code I get an error of "undefined reference to 'c_crypting'"

1 Answer 1

2

This line in your header file:

extern CCrypt *c_crypting;

does not create a c_crypting pointer. It only says "one of my code modules will have a pointer to CCrypt that is called c_crypting" so that the other code files can use that. You'll need to have something like:

CCrypt *c_crypting; // possibly '= 0;'

in one of your .cpp files, and initialize it properly somewhere.

Sign up to request clarification or add additional context in comments.

2 Comments

hmm.. is there a way where i wont initialize the CCrypt*c_crypting in a CPP file?
Well, not really. It has to be initialized somewhere, otherwise it won't point to anything so your code will crash the first time you try to do something with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.