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

"member function already defined or declared"

Started by Silex Jan 31, 2004 at 5:24 PM 12 replies 16.8k views
Original Post
Silex
Silex
I''m getting the "member function already defined or declared" error when I know it can''t be that. I mean, my header and cpp are fairly standard for class in question, i.e.
//header file
class A
{
   public:
   A( ...args... );
};

//cpp file
#include "header.h"
A::A( ...args... )
{
   ... init ...
}
 
so I assume it has to be something else confusing the compiler, but the question is what. I know something pretty bad has to be wrong because when I use the scope resolution operator (i.e. A:: ) in the cpp it doesn''t come up with the contents of A. I''d expect that sort of behavior if my class wasn''t defined properly, or if it couldn''t find the header file, but I''ve double checked those and they seem fine. Has anyone experienced this sort of problem before or know what could be causing it? I do include a bunch of other classes in A''s header, but even when I stop including them the problem persists, so I''m out of ideas. Oh yeah, I also tried changing class and file names but that didn''t help.
Fruny
Fruny
Post an actual, minimal, example that produces that error.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Silex
Silex
That''s the problem, I don''t know what''s wrong. But if you want to know the error that''s being REPORTED (even though I''m convinced that''s not the REAL error), then an example would be:


// C2535.cpp
class C {
public:
void func(); // forward declaration
void func() { // C2535, delete the forward declaration to resolve
}
};

int main() {
}
alnite
alnite
quote:
Original post by Silex

// C2535.cpp
class C {
public:
void func(); // forward declaration
void func() { // C2535, delete the forward declaration to resolve
}
};

int main() {
}

The compiler was correct. You declare func() twice.
Silex
Silex
That was just an example (not from my program) of code that causes the reported error.
Nervo
Nervo
The function declaration within the class is not even overloaded...So if you want to declare and define the function in the class, then why not just write the function out instead of having the declaration twice within the class? You can declare in the class and do the definition outside the class with the :: operator.

EDIT: I think it boils down to you actually posting a real snippet of the class instead of your abstract code..

[edited by - nervo on January 31, 2004 7:50:33 PM]
Well, R2D22U2..
Silex
Silex
I appreciate those who've tried to help, but I think I need to make myself clearer: MY PROBLEM IS NOT HOW TO WRITE A CLASS. As I've already stated, the error REPORTED is NOT the ACTUAL error. This ACTUAL error is what I'm trying to trace down, NOT how to solve the REPORTED error. I'm guilty of this too, but please read the TEXT as well as the CODE, which I posted for purly illustrative purposes.

ANSWER THIS QUESTION: What could be causing the compiler to report this error, assuming THE ERROR REPORTED is not the ACTUAL error?

EDIT: Nervo, I would if I thought it would help, but the class causing the error is huge, and uses a number of other classes that are also huge. I basically need someone who's had this problem before.

[edited by - Silex on January 31, 2004 7:53:36 PM]
chronos
chronos
I''m thinking of an even number. What number am I thinking of?
Kylotan
Kylotan
Silex, you''re just gonna have to post code, because all we can say is that something is probably duplicated somewhere, maybe in a system header, and there''s no way we can find that without knowing what identifiers you''re talking about. I mean, you won''t even tell us which member function the error refers to, which could be a help.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
Silex
Silex
As a wise fat-ass once said, "Screw you guys, I''m goin'' home."

Seriously though, I''m going to change my class layout and hopefully that will solve my problem much faster than it would take you guys to understand my cryptic code.
alnite
alnite
Heh, we are not going to steal your code or anything.

well, anyway good luck, hope you can find the solution.
Auron
Auron
At least from the example you gave, the problem is that you can''t use forward declarations (or prototypes) inside of a class. You either define the function inline (and hence declare it simultaneously) in the class or you declare it in the class and define it outside. There are no prototypes inside classes.

ie:

Bad:

class A
{
void func ();
void func () { blahblah; }
};


Good:

class B
{
void func () { blahblah; }
};


Also good:

class C
{
void func ();
};

void C::func () { blahblah; }


Those are the only options you really have here.

If you''re still adamant that the compiler is lying to you (have you tried another compiler perhaps? They usually don''t have coincident errors...), look for missing semi-colons, un-paired braces, etc.
Qw3r7yU10p!
Qw3r7yU10p!
if you don't have include guards on your headers it can cause problems.

there will be an error in your code which you're not seeing and are unable to describe. show us the code!

or take the files in question, make a copy of them and a new project. strip away a little bit at a time until it compiles. That will give you information. Eventually you could eliminate everything in the class, in which case you won't get any errors. If you still can't understand why the error was coming up, show us that minimal code that still gives the error.

This is a good technique which is worth getting used to using for really perplexing problems.

[edited by - petewood on February 2, 2004 4:17:37 AM]

Topic Locked

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

Sign in to reply to this topic.