Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error : aggregate 'first one' has incomplete type and cannot be defined

Tags:

c++

codeblocks

I have written this header file (header1.h):

#ifndef HEADER1_H
#define HEADER1_H

class first ;

//int summ(int a , int b) ;



#endif

and this source files (header1.cpp and main.cpp):

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

using namespace std;


class first
{
    public:
  int a,b,c;
  int sum(int a , int b);

};

  int first::sum(int a , int b)
{

    return a+b;
}

 

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


using namespace std;


   first one;

int main()
{
   int j=one.sum(2,4);
    cout <<  j<< endl;
    return 0;
}

But when I run this program in codeblocks , I give this Error :

aggregate 'first one' has incomplete type and cannot be defined .

like image 272
ggcodes Avatar asked Jul 23 '13 01:07

ggcodes


2 Answers

You can't put the class declaration in the .cpp file. You have to put it in the .h file or else it's not visible to the compiler. When main.cpp is compiled the type "first" is class first;. That's not useful at all because this does not tell anything to the compiler (like what size first is or what operations are valid on this type). Move this chunk:

class first
{
public:
    int a,b,c;
    int sum(int a , int b);
};

from header1.cpp to header1.h and get rid of class first; in header1.h

like image 70
Borgleader Avatar answered Sep 26 '22 19:09

Borgleader


If you're using a main function as well, just define the class at the top and define the main later. It is not necessary to explicitly create a separate header file.

like image 27
Arpan Adhikari Avatar answered Sep 22 '22 19:09

Arpan Adhikari