Skip to main content
GameDev.net gamedev.net
🔒 Locked

Simple problem

Started by shalom Jan 7, 2002 at 10:56 AM 11 replies 800+ views
Original Post
shalom
shalom
I''m having a simple problem. Its really simple and I''m sure every C programmer in the world knows the answer, but I don''t. I actually asked this question a while ago, but I didn''t ask it very well. I reduced the problem to a simple program. Basically: what''s wrong with the following program: Main.cpp:
  
#include <iostream>
using namespace std;

#include "classa.h"
#include "classb.h"
#include "func.h"

int main()
{
	a A;
	b B;
	cout << add(A,B);
	return 0;
}
  
classa.h:
  
class a 
{
public:
	 int A;
	 a();
	 
};
  
classa.cpp:
  
#include "classa.h"

a::a()
{
	A=5;
}
  
classb.h:
  
class b
{
public:
	int B;
	b();
};
  
classb.cpp:
  
#include "classb.h"

b::b()
{
	B=1;
}
  
func.h:
  
int add(a A, b B);
  
func.cpp:
  
#include "func.h"
int add(a A, b B)
{
	return A.A+B.B;
}
  
Thankx in advanced. In my thunking device, what happens to the inherited pointer to the original base class when I override the function & how do I access it in my inline assembly code, assuming that we are referencing the higher byte of the 16-bit variable?
In my thunking device, what happens to the inherited pointer to the original base class when I override the function & how do I access it in my inline assembly code, assuming that we are referencing the higher byte of the 16-bit variable?
shalom
shalom
For some reason my carriage returns didn''t all appear. Also, I forgot to mention the errors I got:

--------------------Configuration: test - Win32 Debug--------------------
Compiling...
func.cpp
c:\dir\test\func.h(1) : error C2065: ''a'' : undeclared identifier
c:\dir\test\func.h(1) : error C2146: syntax error : missing '')'' before identifier ''A''
c:\dir\test\func.h(1) : error C2059: syntax error : '')''
C:\dir\test\func.cpp(2) : error C2146: syntax error : missing '')'' before identifier ''A''
C:\dir\func.cpp(2) : error C2086: ''add'' : redefinition
C:\dir\test\func.cpp(2) : error C2059: syntax error : '')''
C:\dir\test\func.cpp(3) : error C2143: syntax error : missing '';'' before ''{''
C:\dir\test\func.cpp(3) : error C2447: missing function header (old-style formal list?)
Error executing cl.exe.

test.exe - 8 error(s), 0 warning(s)

It clearly seams to think that the class called ''a'' doesn''t exist.

In my thunking device, what happens to the inherited pointer to the original base class when I override the function & how do I access it in my inline assembly code, assuming that we are referencing the higher byte of the 16-bit variable?
In my thunking device, what happens to the inherited pointer to the original base class when I override the function & how do I access it in my inline assembly code, assuming that we are referencing the higher byte of the 16-bit variable?
shalom
shalom
First of all, I''m having compiler errors (though it could be you posted your message before I posted my second).
Second of all, I don''t think that''s a problem, I don''t need to access the classes by reference; I''m not changing them.

In my thunking device, what happens to the inherited pointer to the original base class when I override the function & how do I access it in my inline assembly code, assuming that we are referencing the higher byte of the 16-bit variable?
In my thunking device, what happens to the inherited pointer to the original base class when I override the function & how do I access it in my inline assembly code, assuming that we are referencing the higher byte of the 16-bit variable?
draqza
draqza
umm... func.h should include the two class headers, and they should probably have the #ifndef ... #define ... #endif. That''s what the undeclared identifier error is telilng you I think.
--


WNDCLASSEX Reality;
...
...
Reality.lpfnWndProc=ComputerGames;
...
...
RegisterClassEx(&Reality);


Unable to register Reality...what''s wrong?
---------
Dan Upton
http://0to1.org
http://www20.brinkster.com/draqza

AIM: DigytalSpyder
WNDCLASSEX Reality;......Reality.lpfnWndProc=ComputerGames;......RegisterClassEx(&Reality);Unable to register Reality...what's wrong?---------Dan Uptonhttp://0to1.orghttp://www20.brinkster.com/draqza
shalom
shalom
I tried, it doesn''t make a difference. the #ifndef stuff just protects against double definition (which I do have in my real program) and #includeing again just doubly defines it.

In my thunking device, what happens to the inherited pointer to the original base class when I override the function & how do I access it in my inline assembly code, assuming that we are referencing the higher byte of the 16-bit variable?
In my thunking device, what happens to the inherited pointer to the original base class when I override the function & how do I access it in my inline assembly code, assuming that we are referencing the higher byte of the 16-bit variable?
Spearhawk
Spearhawk
Umm, should the class and function be declared like this?

    
class b
{
public:
int B;
void b();
};



#include "classb.h"

void b::b()
{
B=1;
}





Edited by - Spearhawk on January 7, 2002 3:07:18 PM
hewhay
hewhay
You must change your func.h to
   
#include "classa.h"
#include "classb.h"

int add (a A, b B);


btw your add function will be more eficient like this

int add (const a & A, const b & B);
humanity will always be slaved by its ignorance
shalom
shalom
Spearhawk - constructors can''t have any return type, not even void.

hewhay & draqza: It worked! Sorry for not believing you before draqza, but I tried it and this time it worked. For some reason, if I just include the class headers, it comes up with double definition errors, but when I include the #ifndef stuff, it not only doesn''t come up with double defenition errors, it doesn''t come up with errors at all! I''m not exactly sure why this works, after all, does it need to include it or not? I thought #ifndef is only a safety precaution - there must be a way of doing it without #ifndef. Whatever, I don''t really care as long as it works.

Thanks.

In my thunking device, what happens to the inherited pointer to the original base class when I override the function & how do I access it in my inline assembly code, assuming that we are referencing the higher byte of the 16-bit variable?
In my thunking device, what happens to the inherited pointer to the original base class when I override the function & how do I access it in my inline assembly code, assuming that we are referencing the higher byte of the 16-bit variable?
shalom
shalom
I forgot (don''t I keep forgetting), why would constant references be more effecient, exactly?

In my thunking device, what happens to the inherited pointer to the original base class when I override the function & how do I access it in my inline assembly code, assuming that we are referencing the higher byte of the 16-bit variable?
In my thunking device, what happens to the inherited pointer to the original base class when I override the function & how do I access it in my inline assembly code, assuming that we are referencing the higher byte of the 16-bit variable?
hewhay
hewhay
first the include pre-compiler directive works by replacing the line #include <filename> with the content of filename so if there are more than one includes with the same filename on the program there will be a multiple delcaration of everything in the file included. The ifndef elseif if ... precompiler directives works by removing or showing the tex after it example if there is an ifndef WORD and word is not defined then everithing between the ifndef and endif stays on code otherwise it is removed before compilation.

Now about the const references, if you don''t use references or pointers every parameter will be copied, ex.
  
int func(int a);
int b;
func (b);


will cause a copy of b to be created for the function.

Now if you use
  
int func(const int & a);
int b;
func (b);


then only the reference to the same a, instead of a copy a, will be used.
With the primitives: int, long, char ...; it doesn''t matter to much but with objects it makes a diference. The general rule is that if an object is not going to be changed better use a reference. If you want to see the difference try this:

  
#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

int main (void)
{
string* ptr;
string var;
cout << "Size of veriable: " << sizeof (var) << endl
<< "Size of reference: " << sizeof (ptr);
return 0;
}


I didn''t try it but i guess it will show the point. With parameters by value you get an space of sizeof (type) * 2 and with references you get sizeof (type) + siseof (ptrType) or something like it. At least that is what i understood on my class of it.
humanity will always be slaved by its ignorance
origil
origil
Not sure about the errors yet but shouldn''t the variable inside the classes be "private:" to the class?
Spearhawk
Spearhawk
quote:
Original post by shalom
Spearhawk - constructors can''t have any return type, not even void.



Ahw, I knew it was something I missed . Oh well back to studdying.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.