Mihai
Mihai

Reputation: 67

C++ Header in Header / objects initalization

I am getting those 2 errors :

-error: expected identifier before numeric constant

-error: expected ',' or '...' before numeric constant

I try from the second class to have an object to first class with parameters and it gives me those 2 errors. Without parameters works fine. This is main:

#include<iostream>

#include "c1.h"
#include "c1.cpp"

#include "c2.h"
#include "c2.cpp"

using namespace std;

int main()
{
    c2 obj2();
    return 0;
}

This is first class header:

#ifndef C1_H
#define C1_H


class c1
{
    public:
        c1(int,int);
};

#endif // C1_H

And its cpp file:

#include "c1.h"

c1::c1(int x,int y)
{
    std::cout << "\nCtor c1\n" << x << "\n" << y << "\n";
}

And the second file header:

#include "c1.h"

#ifndef C2_H
#define C2_H

class c2
{
    public:
        c2();
        c1 obj1(10,2);
};

#endif // C2_H

And its cpp:

#include "c2.h"

c2::c2()
{
    std::cout << "\nCtor c2\n";
}

Thank's.

Upvotes: 0

Views: 64

Answers (1)

Sreekar
Sreekar

Reputation: 1025

Don't use CPP files in includes.

For solution to this problem, you can change the object to pointer and use something like c1* obj1=new c1(10,2) and it should work.

A better way of doing this is, add a class member c1* obj1; and use constructor of c2 to really create the c1 object obj1=new c1();

Upvotes: 2

Related Questions