Jump to content
  • Advertisement
Sign in to follow this  
donjonson

help with headder file containing class definition :)

This topic is 4891 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello, I am trying to make a header file with a class definition. I declare the member functions from within the class definition. Should I define the member functions from within that same header file or should I make a second cpp file and include the headder file? right now i am using the 2nd configuration and I cant seem to get it to compile.

Share this post


Link to post
Share on other sites
Advertisement
what error do you get? sounds like you've forgot to put inclusion guards


#ifndef MY_HEADER_INCLUDED
#define MY_HEADER_INCLUDED
//.. teh funkay schtuff!
#endif//MY_HEADER_INCLUDED


in your header :) that usually gives that sort of problems.

Share this post


Link to post
Share on other sites
(header.h)
#pragma once


class myClass
{
public:
myClass();
~myClass();

private:
int classmember;
};

(end of header file )

(header.cpp)

#include header.h

myClass::myClass()
{
classmember = 6;
}

myClass::~myClass()
{

}

(end of cpp file )



the (end of file) and (header.h) means the diffrent documents.
This should work.

Greetings



*edit*
If you are using visual studios you can use pragma once. If not you should use #ifndef #define and #endif. Personally i always youse ifndef/define/endif

Share this post


Link to post
Share on other sites
Quote:
Original post by DigitalDelusion
note that "#pragma once" is very compiler centric... but if you don't care about portability it's a very slick option and actually seems to reduce buildtimes.


heh you beat me before i could edit =)

Share this post


Link to post
Share on other sites
Hmm I did not use any of those tags.

the error I am getting is as follows

the class has a member function called draw, when I try to defin that function in the headder.cpp and compile It tells me that member function was not declared in the class. But it clearly is defined.

lines.h


class Line;

{

public:
double X,Y,Len,Theta;
int T = 1;
Line(HWND, double, double, double, double, int, bool);
Line(HWND, double, double, double, double, int);
Line();
int draw(HWND);
void Create(HWND, double, double, double, double, int, bool);
int draw(HWND);


private:
HWND Hwnd;
};




lines.cpp

#include "lines.h"
#include <math.h>
#include <windows.h>




int Line::draw(HWND Hwnd)
{
int hT = (T / 2);
int virt = false;
int horiz = false;
int diag = false;

int start[] = {(int) X, (int) Y};
int end[] = {(int) (cos(Theta) * Len), (int) (sin(Theta) * Len)};
//check to see if line is horizontle, virticle or diagonal.

virt = (start[1] == end[1]) ? 1 : 0;
horiz = (start[0] == end[0]) ? 2 : 0;
diag = (start[1] == end[1]) && (start[0] == end[0]) ? 3 : 0;
int lino = horiz + virt + diag;

switch (lino)
{
case 1:
{
//draw line horiz
HDC hDC;
hDC = GetDC(Line::Hwnd);
MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);


if ( T > 4)
{
for(int c = 0; c < hT; c++)
{
start[1]++;
end[1]++;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}

start[0] = (int) X;
start[1] = (int) Y;
end[0] = (int) (cos(Theta) * Len);
end[1] = (int) (sin(Theta) * Len);


for(int c = 0; c < hT; c++)
{
start[1]--;
end[1]--;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}
}
ReleaseDC(Hwnd,hDC);
break;
}
case 2:
{
//draw line virt
HDC hDC;
hDC = GetDC(Line::Hwnd);
MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);

if ( T > 4)
{
for(int c = 0; c < hT; c++)
{
start[0]++;
end[0]++;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}

start[0] = (int) X;
start[1] = (int) Y;
end[0] = (int) (cos(Theta) * Len);
end[1] = (int) (sin(Theta) * Len);

for(int c = 0; c < hT; c++)
{
start[0]--;
end[0]--;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}
}
ReleaseDC(Hwnd,hDC);
break;
}
case 3:
{
//draw line diag
HDC hDC;
hDC = GetDC(Line::Hwnd);
MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);

if ( T > 4)
{
int Signx;
int Signy;
if(start[0] < end[0])
Signx = 1;
if(start[1] < end[1])
Signy = 1;
if(start[0] > end[0])
Signy = 0;
if(start[1] > end[1])
Signy = 0;


if (Signx == Signy)
{
for(int c = 0; c < hT; c++)
{
start[0]++;
end[0]++;
start[1]++;
end[1]++;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}

start[0] = (int) X;
start[1] = (int) Y;
end[0] = (int) (cos(Theta) * Len);
end[1] = (int) (sin(Theta) * Len);

for(int c = 0; c < hT; c++)
{
start[0]--;
end[0]--;
start[1]--;
end[1]--;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}
}
if(Signx != Signy)
{
for(int c = 0; c < hT; c++)
{
start[0]++;
end[0]++;
start[1]--;
end[1]--;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}

start[0] = (int) X;
start[1] = (int) Y;
end[0] = (int) (cos(Theta) * Len);
end[1] = (int) (sin(Theta) * Len);

for(int c = 0; c < hT; c++)
{
start[0]--;
end[0]--;
start[1]++;
end[1]++;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}
}

}
ReleaseDC(Hwnd,hDC);
break;
}
}

return 0;
}



Line::Line(HWND lHwnd, double lx, double ly, double lLen, double lTheta, int lT, bool drw)
{
X = lx;
Y = ly;
Len = lLen;
Theta = lTheta;
T = lT;
Hwnd = lHwnd;


if(drw)
draw(Hwnd);
}

Line::Line(HWND lHwnd, double lx, double ly, double lLen, double lTheta, int lT)
{
X = lx;
Y = ly;
Len = lLen;
Theta = lTheta;
T = lT;
Hwnd = lHwnd;
}

Line::Line()
{
}


void Line::Create(HWND lHwnd, double lx, double ly, double lLen, double lTheta, int lT, bool drw)
{
X = lx;
Y = ly;
Len = lLen;
Theta = lTheta;
T = lT;
Hwnd = lHwnd;


if(drw)
draw(Hwnd);
}





Share this post


Link to post
Share on other sites
added the inclusion guard and fixed the double declaration of draw. I am still getting the same compil error.

The compiler stops at the first use of draw() in the lines.cpp file.

the exact error is as follows

"In constructor `Line::Line(HWND__*, double, double, double, double, int, bool)':
`draw' undeclared (first use this function) "

Now that is just not true because it is declared in the class definition.

am I doing something wrong?

here is the current code.

lines.h

#include <math.h>
#ifndef _LINES_HEADDER
#define _LINES_HEADDER
class Line;

{

public:
double X,Y,Len,Theta;
int T = 1;
Line(HWND, double, double, double, double, int, bool);
Line(HWND, double, double, double, double, int);
Line();
int draw(HWND);
void Create(HWND, double, double, double, double, int, bool);


private:
HWND Hwnd;
};


#endif





lines.cpp

#include "lines.h"
#include <math.h>
#include <windows.h>







Line::Line(HWND lHwnd, double lx, double ly, double lLen, double lTheta, int lT, bool drw)
{
X = lx;
Y = ly;
Len = lLen;
Theta = lTheta;
T = lT;
Hwnd = lHwnd;


if(drw)
draw(Hwnd);
}

Line::Line(HWND lHwnd, double lx, double ly, double lLen, double lTheta, int lT)
{
X = lx;
Y = ly;
Len = lLen;
Theta = lTheta;
T = lT;
Hwnd = lHwnd;
}

Line::Line()
{
}


void Line::Create(HWND lHwnd, double lx, double ly, double lLen, double lTheta, int lT, bool drw)
{
X = lx;
Y = ly;
Len = lLen;
Theta = lTheta;
T = lT;
Hwnd = lHwnd;


if(drw)
draw(Hwnd);
}


int Line::draw(HWND Hwnd)
{
int hT = (T / 2);
int virt = false;
int horiz = false;
int diag = false;

int start[] = {(int) X, (int) Y};
int end[] = {(int) (cos(Theta) * Len), (int) (sin(Theta) * Len)};
//check to see if line is horizontle, virticle or diagonal.

virt = (start[1] == end[1]) ? 1 : 0;
horiz = (start[0] == end[0]) ? 2 : 0;
if((virt + horiz) == 0)
diag = 3;
else
diag = 0;

int lino = horiz + virt + diag;

switch (lino)
{
case 1:
{
//draw line horiz
HDC hDC;
hDC = GetDC(Line::Hwnd);
MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);


if ( T > 4)
{
for(int c = 0; c < hT; c++)
{
start[1]++;
end[1]++;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}

start[0] = (int) X;
start[1] = (int) Y;
end[0] = (int) (cos(Theta) * Len);
end[1] = (int) (sin(Theta) * Len);


for(int c = 0; c < hT; c++)
{
start[1]--;
end[1]--;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}
}
ReleaseDC(Hwnd,hDC);
break;
}
case 2:
{
//draw line virt
HDC hDC;
hDC = GetDC(Line::Hwnd);
MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);

if ( T > 4)
{
for(int c = 0; c < hT; c++)
{
start[0]++;
end[0]++;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}

start[0] = (int) X;
start[1] = (int) Y;
end[0] = (int) (cos(Theta) * Len);
end[1] = (int) (sin(Theta) * Len);

for(int c = 0; c < hT; c++)
{
start[0]--;
end[0]--;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}
}
ReleaseDC(Hwnd,hDC);
break;
}
case 3:
{
//draw line diag
HDC hDC;
hDC = GetDC(Line::Hwnd);
MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);

if ( T > 4)
{
int Signx;
int Signy;
if(start[0] < end[0])
Signx = 1;
if(start[1] < end[1])
Signy = 1;
if(start[0] > end[0])
Signy = 0;
if(start[1] > end[1])
Signy = 0;


if (Signx == Signy)
{
for(int c = 0; c < hT; c++)
{
start[0]++;
end[0]++;
start[1]++;
end[1]++;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}

start[0] = (int) X;
start[1] = (int) Y;
end[0] = (int) (cos(Theta) * Len);
end[1] = (int) (sin(Theta) * Len);

for(int c = 0; c < hT; c++)
{
start[0]--;
end[0]--;
start[1]--;
end[1]--;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}
}
if(Signx != Signy)
{
for(int c = 0; c < hT; c++)
{
start[0]++;
end[0]++;
start[1]--;
end[1]--;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}

start[0] = (int) X;
start[1] = (int) Y;
end[0] = (int) (cos(Theta) * Len);
end[1] = (int) (sin(Theta) * Len);

for(int c = 0; c < hT; c++)
{
start[0]--;
end[0]--;
start[1]++;
end[1]++;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}
}

}
ReleaseDC(Hwnd,hDC);
break;
}
}

return 0;
}



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!