including multiple user defined headers leads to a problem

Hello,
I have recently "got back" to program in C++ after a long period, and i have been dealing with programming c++ which integrated with OpenCv.

I have defined multiple headers, and when i wanted to use all the headers in one specified header "A.h", i got an error which states something as follows:
"No specified constructor for class B", even though in "A.cpp" i have did as included "A.h" as follows:

#include "A.h"

What is the problem, and how do i solve it?

note: #include "B.h" is stated in "A.h" header file.
Last edited on
What's the exact error message?
Where does the error occur? In which line of which file? What is the code there?
Sounds like you have recursive includes. i.e. a.h include b.h and b.h includes a.h.

The way around that is to declare one of the two classes forward. However, when you do that you can only use the forwarded class as a pointer or reference, not by value until the full declaration for the forwarded class has been seen by the compiler.
http://stackoverflow.com/questions/396084/headers-including-each-other-in-c
The problem is that i don't have any recursive includes
The exact error is :

error C2512: 'Oracle' : no appropriate default constructor available.
error C2512: 'Ellipsoid' : no appropriate default constructor available.
error C2512: 'EpsilonSampler' : no appropriate default constructor available.
IntelliSense: no default constructor exists for class "Oracle".

these errors appears in a class A ( different from Oracle) but A includes Oracle.h
Can you post minimal code the reproduces the issue?
Are you inheriting from the Oracle class? If so, what does that inheritance look like?

Since Oracle apparently has no default constructor, any class that inherits from Oracle will have to initialize Oracle using a non-default constructor. Ditto for the other two classes.

Please show us oracle.h and any constructor definitions that inherit from Oracle.




Last edited on
"'Oracle' : no appropriate default constructor"

This indicates that something is trying to create an object of type Oracle, using the default constructor, but that default constructor does not exist.

As AbsAnon says, I most commonly see this where someone has inherited from Oracle, and is now trying to create an instance of the inherited class, and hasn't told their inherited class' constructor which Oracle constructor to use.

You need to identify which Oracle constructor to use, and put it in the initialisation list of the derived class.

http://stackoverflow.com/questions/710432/no-appropriate-default-constructor-available-error-in-visual-c

Thanks alot, i did create an object of Oracle that uses default constructor, by only defining the variable:

Oracle oracle; // in A.h

Moreover i did create an object of Mat but i have defined it as external variable so every method in other files can use this variable and edit it too.

But i have an error which states:

error LNK2001: unresolved external symbol "class cv::Mat sampledMap" (?sampledMap@@3VMat@cv@@A)

note: i defined this variable in Oracle as:
extern Mat sampledMap;

But i initialized it in A.h.

What is the problem here?

@Zhuge asked:
Can you post minimal code the reproduces the issue?


Your posts have sounded a lot like "I have done everything right, but it doesn't work." You need to show us the code that's causing you problems so we can help.

Take your code that fails to compile, remove everything that is unrelated, verify that the error still exists, and post the code here. It will make it so much easier to help you debug your code if we can see the code. Saying, for instance,
note: i defined this variable in Oracle as:
extern Mat sampledMap;

But i initialized it in A.h.
is pretty hard for us to check. You probably made a mistake in the way you initialized sampledMap (for instance, it needs to be in a source file, not a header file), but we can only guess at what mistakes you may be making until you post some code.

Last edited on
i did create an object of Oracle that uses default constructor, by only defining the variable:
 
Oracle oracle; // in A.h  


The compiler is telling you that class Oracle does not have a default constructor. That line requires a default constructor. What constructor do you think is going to be called?

Moreover i did create an object of Mat but i have defined it as external variable so every method in other files can use this variable and edit it too.

But i have an error which states:

error LNK2001: unresolved external symbol "class cv::Mat sampledMap" (?sampledMap@@3VMat@cv@@A)

note: i defined this variable in Oracle as:
extern Mat sampledMap;

That's not a definition. That's a declaration that states sampledMap exists in another module.
You must instantiate sampledMap somewhere (without the keyword extern).
The problem was (for the default constructor) is that i have a created non default constructor thus i apologize for asking before checking.

i have initialized sampledMap by getting it from Oracle.h (in this file it was defined as earlier) in A.cpp as follows:

sampledMap = (Mat) Mat::zeros(oracle.getSize(), CV_32S);
Last edited on
You must instantiate sampledMap somewhere

Somewhere that preferably is not a header file, because headers can be included into multiple translation units and a definition may not exist in more than one.

One obviously will have to link all necessary object files.


The use of global variables (like smapledMap) is usually discouraged, because they restrict the program (in addition to issues that you did encounter).

CPlusPlusNoobie1 wrote:
The exact error is :
error C2512: 'Oracle' : no appropriate default constructor available.

While I have no love for the build system that you do use, I still do know that that compiler can say more. It mentions filename and line number. Or should.

While some syntax errors mess compiler's heuristics so badly that it fails to point to the spot of the real error, compiler generally does good job at it. In this case, did it point to file A.h and line that has that:
Oracle oracle;
(An another global variable?)

That code was relevant for debugging and so was the definition of class Oracle too. A class has multiple members (constructors and the destructor) that the compiler generates automatically, if they have not been explicitly written. However, not always. The rules were relatively simple before C++11 and its move constructors. One is that if a class has any constructor, then compiler does not create the default constructor at all.
The only global variable i have is sampledMap!
If i should not use global, what do you propose?

what build system are you talking about, if i may ask ?

Thanks in advance.
Thanks anyway but i have removed the use of global so that the the code will be "smooth" and better.

But i want to know something, why using global variables is bad?
When do they start their live?
When do they die?
What memory area do they occupy?
In which order, if they are defined in different translation units?

What if construction or destruction throws an exception? How to catch that?

Are they thread-safe?


Compare:
1
2
3
4
5
6
7
void foo() {
  // uses global bar
}

void gaz( T & bar ) {
  // uses argument bar
}

The foo() is bound to one single global variable. It can serve no other purpose.
The gaz() can be called with any and all objects of type T. It is more generic. It can even be used in place of foo(). The foo() is redundant.
Topic archived. No new replies allowed.