How to correctly extend a class in C++ and writing its header file?
I'm pretty new to C++ and I'm trying to create my first little project. I
encounter some problems during compilation. It says C2504 Person: base
class undefined. I know that, in a header file, I need to declare or
include tha class that I'm extending in the correspondent *.cpp file. I'll
write the code to understand better.
main.cpp
#include <Windows.h>
#include "client.h"
using namespace person;
using namespace std;
int __stdcall WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int cmdShow)
{
try
{
Person *p = new Client;
// DO STUFF
}
catch(Exception &e)
{
//Error, so let's exit 1
return 1;
}
//No errors, so we return with 0
return 0;
}
client.cpp
#include <time.h>
#include <ctype.h>
#include <string>
#include "client.h"
using namespace person;
using namespace std;
class Client : public Person {
public:
Client()
{
//DO STUFF
}
void onMessage(const char * const message)
throw(Exception &)
{
//DO STUFF
}
private:
void gen_random(char *s, const int len) {
//DO STUFF
}
};
client.h
#ifndef CLIENT_H
#define CLIENT_H
#include "person.h"
class Client : public Person {
public:
Client();
void onMessage(const char * const message) throw(Exception &);
private:
void gen_random(char *s, const int len);
};
No comments:
Post a Comment