user2136963
user2136963

Reputation: 2606

Problems with struct prototyping (Invalid use of undefined type) c++

I want my caba stucture to contain a pointer to a variable of aba structure. And I also want aba structure to do some operations depending of properties of set < caba >.

But when I use properties of aba pointer inside caba, I get an error

#include<stdio.h>
#include<set>
using namespace std;
struct aba;
struct caba
{
    aba *m;
    int z;
    bool operator >(const caba &other)
    {
        if(m==NULL||other.m==NULL)
            return true;
        return (*m).x>(*(other.m)).x;
    }
};
set <caba> t;
struct aba
{
    int x,y;
    bool f()
    {
        return !t.empty();
    }
};

int main()
{
    return 0;
}

sayng:

In member function `bool caba::operator>(const caba&)':

Test.cpp|13|error: invalid use of undefined type `struct aba'

Test.cpp|4|error: forward declaration of `struct aba'

Test.cpp|13|error: invalid use of undefined type `struct aba'

Test.cpp|4|error: forward declaration of `struct aba'

But why is aba undefined? There is a prototype of it.

Upvotes: 0

Views: 894

Answers (1)

juanchopanza
juanchopanza

Reputation: 227478

You have declared aba, but your code needs the definition too. What you can do is move the offending code out of the caba class definition and into a .cpp implementation file which includes both aba.h and caba.h.

// caba.h (include guards assumed)
struct aba;
struct caba
{
    aba *m;
    int z;
    bool operator >(const caba &other);
};

//caba.cpp
#include "caba.h"
#include "aba.h"
bool caba::operator >(const caba &other)
{
    if(m==NULL||other.m==NULL)
        return true;
    return (*m).x>(*(other.m)).x;
}

Upvotes: 2

Related Questions