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

Weird C++ compiler error

Started by chipmunk1886 Jan 15, 2003 at 5:20 PM 7 replies 1.9k views
Original Post
chipmunk1886
chipmunk1886
I've been hacking at this thing for an hour. The compiler is .NET. The error I keep getting is: error C2143: syntax error : missing ',' before '&'. in reference to some function arguments. The problem lies with two file Vertex.h and Vector3.h. In Vertex there is a class named Vertex3 and it has an overloaded operator: Vertex3 operator +(const Vector3& vector) const { Vertex3 result(_x +vector.x(), _y +vector.y(), _z +vector.z() ); return result; } The 'const Vector3&' is the '&' the error is refering too. The argument is a vector so I've included Vector3.h for the Vector3 class. In vector3.h I have another problem function in the Vector3 class. This uses the Vertex3 class so i've included vertex.h: float Dot(const Vertex3& vertex) const //Dot product { return float(_x * vertex.x()+ _y + vertex.y()+ _z * vertex.z() ); } I get the same error for this function. But depending on how I include the header files in each other or in other header files, I can make the second error go away. The first function generates the same error 5 times while the second only generated 1. The files are defined correctly and it's able to find everything it needs. All the code is fairly plain, no really fancy OOP relationships. I must be mixing something up with the include files? I'll check up on this tonight, so if you need more info I'll respond promptly. Thanks in advance! [edited by - chipmunk1886 on January 15, 2003 7:04:25 PM] [edited by - chipmunk1886 on January 15, 2003 7:35:57 PM]
-Ose
chipmunk1886
chipmunk1886
i know, it''s just a syntax error, but I still don''t know what the problem is. You''ll probably figure it out easily.
-Ose
chipmunk1886
chipmunk1886
okay these two sets of code show the interaction between two classes A and B
This is the code for class A

    

#ifndef ASDF_H
#define ASDF_H

#include "qwer.h"

class A{
public:
A();
A(int _x)
{
x = _x;
}

float xx()
{
return x;
}

A add(const B& y)
{
return A (x+y.yy());
}

private:
int x;
};

#endif


this is class B

#ifndef QWER_H
#define QWER_H

#include "asdf.h"

class B{
public:
B();
B(int _y)
{
y = _y;
}
int yy()
{
return y;
}

B mult(const A& x)
{
return B (x.xx()*y);
}
private:
int y;
};
#endif


This code, working each in separate header files, generates the same error. It doesn't like class A using class B or Class B using class A for any functions. This is as simple as I can spell it out. Please help, this is driving me nuts haha.

[edited by - chipmunk1886 on January 15, 2003 9:36:14 PM]
-Ose
chipmunk1886
chipmunk1886
okay. I''ll take a stab. Creating classes that both use eachother in some way (in particular as function arguments) is very bad. I guess if two classes are so involved with one another they should be combined or inherited? Is that right? Was this question so naive that it couldn''t even be answered?
-Ose
Sneftel
Sneftel
Nah, you can do that, and inheritance is not what you want. The trick is to separate interface and implementation. Define your classes in 2 .h files, and implement the functions in 2 .cpp files. Each .cpp file should include both headers.


Don''t listen to me. I''ve had too much coffee.
chipmunk1886
chipmunk1886
sorry, using a .cpp doesn''t make a difference. Same error. I understand what you meant about organization, but that code block was only an example. I don''t put source in the header files normally. I think the relationship between the two classes was just improper. thanks
-Ose
Qw3r7yU10p!
Qw3r7yU10p!
put the implementation of the functions in a cpp file

put a forward declaration of B and A in the opposite files to let the compiler know about their existance.

class A;// in B.h
class B;// in A.h
MadKeithV
MadKeithV
What petewood said.

But I''d add that probably you''ve introduced a design hazard by making the classes dependent on eachother. I don''t see why vector should know about vertex - a vector can be used for many things (including vectors) that it need not know about.
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
chipmunk1886
chipmunk1886
thanks guys. yeah, I changed the problem functions so that they just aren''t dependent on each other. it works out better anyhoo.
-Ose

Topic Locked

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

Sign in to reply to this topic.