0

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.

3
  • 3
    You should almost never have #include "xxxx.cpp" Commented Nov 16, 2015 at 20:43
  • What are you trying to do on this line: c1 obj1(10,2);? Commented Nov 16, 2015 at 20:46
  • Then what to #include ? To create an object with parameters to first class which is c1. Commented Nov 16, 2015 at 20:54

1 Answer 1

2

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();

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, with pointers worked perfectly fine. But I didn't understand the "better way". What do you mean with that? Btw, with local variable, doesn't work? only with pointer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.