Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: has initializer but incomplete type

Tags:

c++

I am having the problem: has initializer but incomplete type while using struct:

in a hpp file:

class A
{
private:
   struct videoDT;
};

in a cpp file:

struct A::videoDT
{
  videoDT(int b) : a(b){}

  int a;
};

void test()
{
   struct videoDT test(1);
}

Then I have the problem:

Error: has initializer but incomplete type

Thanks in advance

like image 330
olidev Avatar asked Nov 26 '10 17:11

olidev


2 Answers

I think the problem is that test() doesn't have access to A's private types.

This compiles for me:

class A
{
private:
    friend void test();
    struct videoDT;
};

struct A::videoDT
{
    videoDT(int b) : a(b){}

    int a;
};

void test()
{
    A::videoDT test(1);
}
like image 106
sbi Avatar answered Sep 25 '22 12:09

sbi


In your test function you declare a local type struct videoDT, but never define it. Not surpisingly, the compiler complains about an object of incomplete type being initialized. End of story.

How did you expect it to work? If you wanted your declaration to use A::videoDT type, then you should have used the qualified name for the type - A::videoDT - since that's what that type is called. However, the code will not compile anyway, since A::videoDT is private in A and test has no access to it.

In other words, it is hard to figure out what you were trying to do. Provide some explanations or code that make more sense.

like image 41
AnT Avatar answered Sep 21 '22 12:09

AnT