|
Ok I got it to work, thanks.
KR
|
|
|
|
|
I'm having trouble adding inheritance into my program. First here is my code so far:
#include <GL/glut.h><br />
<br />
class Character<br />
{<br />
protected:<br />
int width, height;<br />
int tileX, tileY;<br />
float x, y;<br />
float speed;<br />
public:<br />
Character(float xPos, float yPos);<br />
Character();<br />
~Character();<br />
void draw();<br />
void move(int direction);<br />
float getX();<br />
float getY();<br />
};<br />
<br />
#include "Character.h"<br />
<br />
Character::Character(float xPos, float yPos)<br />
{<br />
width = 50;<br />
height = 50;<br />
x = xPos;<br />
y = yPos;<br />
speed = 0.5;<br />
}<br />
<br />
Character::Character()<br />
{<br />
}<br />
<br />
void Character::draw()<br />
{<br />
glColor3f(1.0, 0.0, 0.0);<br />
glPushMatrix();<br />
glTranslatef(x,y,0);<br />
glBegin(GL_QUADS);<br />
glVertex2d(0, 0);<br />
glVertex2d(0, height);<br />
glVertex2d(width, height);<br />
glVertex2d(width, 0);<br />
glEnd();<br />
glPopMatrix();<br />
}<br />
<br />
void Character::move(int direction)<br />
{<br />
if (direction == 2)<br />
{<br />
x -= speed;<br />
}<br />
else if (direction == 1)<br />
{<br />
x += speed;<br />
}<br />
else if (direction == 3)<br />
{<br />
y += speed;<br />
}<br />
else<br />
{<br />
y -= speed;<br />
}<br />
}<br />
<br />
float Character::getX()<br />
{<br />
return x;<br />
}<br />
<br />
float Character::getY()<br />
{<br />
return y;<br />
}<br />
<br />
#include <GL/glut.h><br />
#include "Character.h"<br />
<br />
class Alien : public Character<br />
{<br />
public:<br />
Alien(float xPos, float yPos);<br />
};<br />
<br />
#include "Alien.h"<br />
<br />
Alien::Alien(float xPos, float yPos)<br />
{<br />
width = 50;<br />
height = 50;<br />
x = xPos;<br />
y = yPos;<br />
speed = 0.1;<br />
}<br />
I thought this was adequate to get Alien to inherit all of the methods of Character, but I get the following errors:
error C2011: 'Character' : 'class' type redefinition
error C2504: 'Character' : base class undefined
error C2039: 'draw' : is not a member of 'Player'
error C2039: 'draw' : is not a member of 'Alien'
and so on. Any ideas what I am doing wrong? Thanks.
|
|
|
|
|
Don't forget to add the precompiler directive at the top of your header files so that they are included only once:
#ifndef MYFILE_H
#define MYFILE_H
#endif
|
|
|
|
|
Ah this fixed it. May I ask what these lines actually do? I've seen these a few times.
|
|
|
|
|
They just prevent that your header file is included several time (thus avoiding a redefinition of an existing class).
Basically, when you do an include, the precompiler will replace the include directive by the contents of the included file. Thus, in certain condition it can happen that you include the same file from different files (if two files include the same file and they are both included in another file).
|
|
|
|
|
I think your problem is that Character.h is being included more than once. Generally, header files are wrapped in something like
#ifndef CharacterClassInclusionGuard
#define CharacterClassInclusionGuard
class Character ( etc)
#endif
What's the other include you have in the header ? Is it stdafx.h ? That should be in the cpp.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
To complete Cedric and Christian answers, there's a Microsoft specific precompiler command that does the same thing, plus an additional check that permits a file not to be inserted (at least tried to be inserted) twice.
at the beginning of the header file you write, you can instead write this :
#pragma once
and that's all.
but don't forget it's microsoft specific, so if you plan to port your code to any other compiler, prefer using the other syntax
|
|
|
|
|
I’m making my first dive into what I’ve heard called “functionoids” or “functoids” (function pointer flexibility without the ‘static’ limitations). So far, so good…
All of the included code works great; that is, until I attempt to access members of the class Foo via a call to my “function pointer”. The ‘this’ pointer allows access to members of Foo::func1; ok fine, that makes perfect sense. But I want to reference members of Foo from the “functionoid” and don’t know how.
What am I missing?
Foo.h:
class Functionoid
{
public:
virtual void go() {}
};
class Foo
{
public:
void MainProc();
bool var;
class func1 : public Functionoid
{
public:
virtual void go();
};
};
Foo.cpp:
#include "Foo.h"
Foo::func1::go()
{
if (this->var)
0;
}
void Foo::MainProc()
{
Functionoid* pf1 = new func1;
pf1->go();
delete pf1;
}
-- modified at 15:37 Tuesday 13th February, 2007
|
|
|
|
|
You can't access any Foo members without an object of type Foo.
Nesting the func1 class declaration in Foo doesn't grant it any access to Foo's members (no
members exist without an object anyway).
How does this work... "Functionoid* pf1 = func1;"
Assigning a pointer from a class name? Maybe there needs to be a member object of type Foo::func1
in the Foo class?
Mark
|
|
|
|
|
Mark Salsbery wrote: How does this work... "Functionoid* pf1 = func1;"
Sorry, that should have read:
Functionoid* pf1 = new func1;
Mark Salsbery wrote: Nesting the func1 class declaration in Foo doesn't grant it any access to Foo's members
I understand that the code as written doesn't work ... my question is this: Is there a way I can change the aforementioned code such that- I might retain the non-static function pointer features, and
- have access to Foo's members?
|
|
|
|
|
I don't know how you'd do that. You need an object to access an object's members.
Nesting a class affects its scope, but not member access.
Functionoids can have constructor though, so an object can be passed:
class Functionoid
{
public:
virtual void go() = 0;
};
class Foo
{
public:
void MainProc();
bool var;
class func1 : public Functionoid
{
Foo &foo;
public:
func1(Foo &fooref) : foo(fooref) {}
virtual void go();
};
};
void Foo::func1::go()
{
if (foo.var)
{
}
}
void Foo::MainProc()
{
Functionoid* pf1 = new func1(*this);
pf1->go();
delete pf1;
}
|
|
|
|
|
That did it!
I'm new enough to C++ to be not-quite-sure what's going on with the "&foo" and " : foo(fooref)" business, but ignorance forces a pursuit of knowledge, so off to the old textbooks I go.
I'll be smarter tomorrow. Thanks!
|
|
|
|
|
quasinormalized wrote: not-quite-sure what's going on with the "&foo" and " : foo(fooref)" business
Since an object is required to have members to access, I chose to pass an object reference.
It could have been a pointer as well.
Warning:
With this method, if the Foo object referenced by the Foo::func1 member (Foo::func1::foo) goes
out of scope or is deleted, then subsequent calls to the go() method will fail if go() trys to
use foo.
In other words, it's up to you to track the lifetime of all objects involved
|
|
|
|
|
hi
i want to convert
String^ abc = "Hello world"
to
char *
or
char []
any idea how to do it?
|
|
|
|
|
|
|
I need help with some homework. I had to write a program that would convert a number entered by the user into another base between 2-10. For example 1014 base 10 to base 8 = 1766. My program seems to convert some bases correctly both other it gives me the wrong answer and i cant figure it out. Also i set a while loop that makes the user enter a base number between 2-10, if they dont it keeps asking them until they do. What happens is if you still enter a number between 2-10 say 8 it will still ask you to enter a number between 2-8 and if you enter 8 again it will work.
thanks for your help.
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
// Variables
int num, base, p=2, d, m;
// User input
cout << "Please enter your number: ";
cin >> num;
// Number less than Zero
while (num <= 0)
{
cout << "Please enter a number greater than zero: ";
cin >> num;
}
cout << "Please enter the base number you would like to convert your number to: ";
cin >> base;
// Base between 2-10
while (base != 2, base != 3, base != 4, base != 5, base != 6, base != 7, base != 8, base != 9, base != 10)
{
cout << "Please enter a base number between 2-10: ";
cin >> base;
m=(int)pow((double)base,(double)p);
// Calculations
if (m > num)
{
p--;
m=(int)pow((double)base,(double)p);
}
else
{
p++;
m=(int)pow((double)base,(double)p);
}
while (p >= 0)
{
d=num/m;
cout << d;
num=num%m;
m=m/base;
p--;
}
cout << endl;
return 0;
}
}
|
|
|
|
|
chipper3344 wrote: while (base != 2, base != 3, base != 4, base != 5, base != 6, base != 7, base != 8, base != 9, base != 10)
I don't know what you are trying to do there but it is wrong. You should separate the different conditions by an AND: while (base!=2 && base!=3 && ...
By the way, something a little bit cleaner would be to check if your number is outside the boundaries (no check every possible case):
while (base<2 || base base>10) (it will continue looping if base is strictly less than 2 OR strictly bigger than 10). But I suppose you want to do the opposite ?
For the rest, I didn't look too much in details.
|
|
|
|
|
While using Visual Studio 2005 to built a MFC C++ application, when I create a Control Variable on a dialog it takes about 5 minutes.
However if I disconnect from our network it works as normal; i.e no long wait.
I have reset all reference to network drives to local drives, does anyone know what is causing this.
It looks like a network timeout, but I dont know the item that its looking for on the network whn creating a ctontrol variable.
Many thanks,
Andy.
|
|
|
|
|
Do your VS2005 default #include or lib paths point to network drives?
/ravi
|
|
|
|
|
No, I don't think so.
If I move the project onto a PC which is not connected to the network as is fine.
I have opted for everything on C:\.
Does that help.
|
|
|
|
|
What about a network drive in your system PATH?
|
|
|
|
|
Just did a check and NO, no ddrives other than C:
Andy.
|
|
|
|
|
I'm learning c++ and using visual studio c++ 2005 express edition. I had written a lot of program's code and try to run, and when it should be started I got an error : An unhandled exception of type 'System.NullReferenceException' occurred in System.Windows.Forms.dll Additional information: Object reference not set to an instance of an object.
What must I do, that this error disapear ???
|
|
|
|
|
You must be new if you're a stranger to the NullReferenceException!
For .NET-related question there's at least two other message boards here that will be more
helpful:
(Managed) C++/CLI[^]
.NET Framework[^]
|
|
|
|