Anton Yershov
Anton Yershov

Reputation: 57

Why am I getting an error telling me the constructor is not naming a type?

I am using Code::Blocks. Here is my code

#include "LargeInt.h"

LargeInt::LargeInt()
{

}

Header

#ifndef LARGEINT_H
#define LARGEINT_H


class LargeInt
{
    public:
        LargeInt();
};

#endif // LARGEINT_H

The error that I am getting is

'LargeInt does not name a type' in line 3 of my class

All I did was click file > new > class and then started coding without changing any settings or anything like that.

Upvotes: 0

Views: 1706

Answers (2)

Joseph Mansfield
Joseph Mansfield

Reputation: 110698

A constructor is supposed to perform any operations needed to get an object of type LargeInt into a valid state. It seems like you're trying to define the functions operator<< and operator+ inside the constructor - you can't do this:

LargeInt::LargeInt()
{
    LargeInt::operator<<(String input){}
    LargeInt::operator+(LargeInt){}
}

You should define each function that has a corresponding declaration from the class definition. Your implementation file should look something like this:

LargeInt::LargeInt()
{
    // ...
}

LargeInt LargeInt::operator<<(String str)
{
    // ...
    return some_large_int;
}

istream& operator>>(istream &is, LargeInt &large)
{
    // ...
    return is;
}

ostream& operator<<(ostream &os, LargeInt &large)
{
    // ...
    return os;
}

Upvotes: 1

Coert Metz
Coert Metz

Reputation: 902

You should not define operators within your constructor. They should be separate methods in your CPP file.

Upvotes: 3

Related Questions