MartinHaTh
MartinHaTh

Reputation: 1447

Simple c++ constructor don't work (very basic)

Ok, so I've decided to read a little c++ now and then, just to get a basic understanding of the syntax. I am familiar with Java, and a little bit of Python. I have 'read' through a c++ for dummies book, and I thought I had the grip - until I tried to create the simplest class. The idea was very simple: a class (named ape) takes one parameter, an int, which is stored as a private field. It has one other function, a return function, which returns the field. The main() creates an instance, and calls the method to print out the variable.

The idea was to use a string instead of an int, but I couldn't get it working, so I decided to use an int instead, which, obviously, wasn't working either.

If it is to any interest I use Code::blocks, Windows 7 and the g++ compiler.

Here are the classes:

Main.cpp

#include <iostream>
#include "ape.h"

using namespace std;

int main()
{
    ape asd(10);
    cout << asd.getNumber();
}

ape.h

#ifndef APE_H
#define APE_H

class ape
{
    public:
        ape(int num);
        virtual ~ape();
        int getNumber();
    protected:
    private:
        int number;
};

#endif // APE_H

and ape.cpp

#include "ape.h"
using namespace std;

ape::ape(int num)
{
    tall = num;
}

ape::~ape()
{
    //dtor
}

int getNumber()
{
    return number;
}

The error messages I get seems very random to me, as they are changing completely with every single change I make, and are not very self explaining. I can see how I sound like an arrogant fool, and that this whole mess is the compilers fault, but I really don't see any connection between the error messages and what's wrong in my code.

Take it easy on me, first time here. :)

I guess I should probably add the error message: undefined reference to 'ape::ape(int)'

Upvotes: 2

Views: 165

Answers (6)

ereOn
ereOn

Reputation: 55726

You are assigning a value to a variable that doesn't exist.

Change:

ape::ape(int num)
{
    tall = num;
}

To:

ape::ape(int num) :
  number(num)
{
}

Moreover, I don't know why you wrote a destructor, and why you decided to make it virtual. By default, the compiler will generate an empty destructor (similar to yours, but not virtual). If you don't intend to do polymorphism (with inheritance and all that "stuff"), you may delete this destructor from your code as it only brings complexity for no gain.

You also need to prefix your method definitions with the name of the class:

int ape::getNumber() // Note the "ape::" here
{
    return number;
}

One last thing:

You problably want to change also your main:

int main()
{
    ape asd(10);
    cout << asd.getNumber() << endl;
}

Something put into cout only gets printed when one outputs endl or flush as the stream is buffered.

Regarding your last edit:

I guess I should probably add the error message: undefined reference to 'ape::ape(int)'

You probably failed to link with ape.o. In your compilation folder, you should have your source files and a .o for every .cpp. You have to link those object files together to build your program. This message indicates that the linker is unable to find the definition for ape::ape(int). That is he was probably not given the ape.o file.

Long story short, here is what your compilation commands should look like (using g++):

g++ -o main.o -c main.cpp
g++ -o ape.o -c ape.cpp
g++ -o program main.o ape.o

Upvotes: 1

duncan
duncan

Reputation: 6283

What is tall? The private field is called number in your example, so this function:

ape::ape(int num)
{
    tall = num;
}

should be:

ape::ape(int num)
{
    number = num;
}

However, in C++ you should use initialization and not assignments. While for an int is not a big deal, for more complex objects assignment is more expensive as you are copying the object, while with initialization you can use the copy constructor. Therefore the convention is to initialize all variables before the body of the constructor.

ape::ape(int num)
  : number(num)
{
}

And, please declare getNumber() as a const method. Here you have a nice guide explaining all the const variants: http://developer.kde.org/~wheeler/cpp-pitfalls.html

While you are at it, add the missing class prefix, which you need if you are defining the method outside of the declaration of the class:

int ape::getNumber() const
{
    return number;
}

As a last comment, I recommend you look for a convention on naming member variables. Typical are mNumber or _number. Makes reading your own code easier later on.

Upvotes: 0

Al Kepp
Al Kepp

Reputation: 5980

You need to set variable number in constructor, before you can read it in getNumber. Try this:

ape::ape(int num)
{
    number = num;
}

Upvotes: 0

Alan Stokes
Alan Stokes

Reputation: 18964

ape::ape(int num)
{
    tall = num;
}

tall? Where did tall come from? The member was declared as number.

Better would be

ape::ape(int num) { number = num; }

Much better would be

ape::ape(int num) : number(num) { }

Upvotes: 0

K-ballo
K-ballo

Reputation: 81349

Not much to say without the actual error messages, but at least here is a problem:

int getNumber()
{
    return number;
}

This should be int ape::getNumber(). Also in your code there is no definition for tall, should perhaps be number instead?

Upvotes: 3

okrasz
okrasz

Reputation: 3976

You are using undeclared field "tall". Seems like your constructor should be:

ape::ape(int num)
{
    number = num;
}

Upvotes: 0

Related Questions