0

Say I have the following:

Foo::Foo() {
value = 25; //default constructor...
}

Foo::Foo(Enum bar) {
value = (int)bar; //purpose is to allow an integer to take enum constant's integer value.
}

from...

enum Enum 
{
A = 25,
B = 50,
}

class Foo 
{
public:
    Foo();
    Foo(Enum bar);
private:
int value;
}

Yet, if I do the following:

Enum bar = A; //A = 25

Foo * foo = new Foo(A); //error: "undefined reference to Foo::Foo(Enum)"

This is in Eclipse CDT 3.6. Why is this happening? Is there anything I can do about this to solve the problem?

8
  • 1
    Foo header file is included ? Commented Jul 21, 2011 at 3:33
  • 5
    enum Enum? Worst naming ever! Commented Jul 21, 2011 at 3:37
  • 1
    you forgot to implement Foo::Foo(Enum); ? Say like the .cpp didn't get compiled properly? Commented Jul 21, 2011 at 3:37
  • @tp1 No, the .cpp is identical to the .h file in terms of method signatures. Commented Jul 21, 2011 at 4:09
  • in what file is Foo::Foo(Enum bar) implemented? Is that file included in the project file (i.e., is it actually being compiled)? Commented Jul 21, 2011 at 4:11

2 Answers 2

4

After fixing a few syntax errors (extra , in Enum definition, missing ; after Enum and Foo definitions), your code was run-able in gcc. Check here: http://www.ideone.com/GZdNM

I don't really understand what you're trying to do with the enum, but for sure, calling it Enum is probably not a great idea...

Sign up to request clarification or add additional context in comments.

1 Comment

That was just an example name. The real Enum name is just "BoostAmount". The idea is to pass the enum value to the constructor, and an integer field takes on the integer value the enum represents. My header file is included via absolute path, so I'm not sure how it works for you but it doesn't for me. If there are any questions I can answer, I'd be happy to.
1

It sounds like you forgot to link foo.C into your final application.

4 Comments

Sorry, I'm not quite following. What do you mean, exactly?
+1. @Holland: If you put the implementation of Foo::Foo(Enum) in a separate file - say foo.c++, then the normal compiler behaviour if you compile that file is to produce foo.o. You then need to tell the compiler to link foo.o when compiling the file containing main(). On UNIX/Linux, you'd normally write a Makefile to do this, but I'm not sure what Eclipse expects.
@Tony Thanks for your assistance, Tony. My Eclipse project is actually setup to use a Makefile to do the compiling. Is there any quick snippet I could write within the Makefile which you know of that could do this?
@Holland: at absolute simplest, you want app: <tab> app.c++ foo.o on one line, <tab> g++ -o app app.c++ foo.o on the next.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.