Error no appropriate default constructor available

Pages: 12
In constructor of MyGlobalClass I try to pass instance of the class MyGlobalClass

1
2
3
4
5
MyGlobalClass::MyGlobalClass(int argc, char* argv[]) 
{
	CLParser * CLPars( MyGlobalClass *);
	FILE_ * File( MyGlobalClass *);
}


Howerver I got this errors:
I got new error:
error C2512: 'CLParser' : no appropriate default constructor available
error C2512: 'FILE_' : no appropriate default constructor available

If I remove the asterisks in constructor arguments I got this error:
error C2819: type 'FILE_' does not have an overloaded member 'operator ->'

Both classes start similar:
#include "stdafx.h"

1
2
3
CLParser::CLParser(  MyGlobalClass * globInst ){
	PGlobalInstance = globInst;
	}


PGlobalInstance must keep reference of the instance of MyGlobalClass which is created in main function.
I think you want to use "this", as in Clpars(this)
Hey! That's great idea! I didn't think about it yet. It could save a lot of problems. But see this:
1
2
3
4
5
6
MyGlobalClass::MyGlobalClass(int argc, char* argv[]) 
	//: CLPars(globInst), FIle(globInst)
{
	CLParser * CLPars( this );
	FILE_ * File( this );
}

This will produce error:
error C2512: 'CLParser' : no appropriate default constructor available
error C2512: 'FILE_' : no appropriate default constructor available.
I tired various header for the constructor:
CLParser::CLParser( MyGlobalClass ) ...
CLParser::CLParser( MyGlobalClass *) ...
or
CLParser::CLParser( MyGlobalClass * globInst) ...
however nothing removed the last error...
Last edited on
Please, show the definition of MyGlobalClass.
1
2
3
4
5
6
7
8
#include "stdafx.h"

MyGlobalClass::MyGlobalClass(int argc, char* argv[]) 
{
	CLParser * CLPars( this );
	FILE_ * File( this );
//	CLParser::ParseArgs(argc, argv);
};
http://www.eelis.net/iso-c++/testcase.xhtml

I asked to my mind reading cat, and he suggest that you look for 'initialization list'.
Also, scope.
That is an implementation of one constructor of MyGlobalClass, not the definition.

Class definition looks like:
1
2
3
4
class Foo // optionally something
{
  // something
};
Ah the, definition of class is here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class MyGlobalClass{
public:
		std::vector<Src*> sources;
		std::vector<Target*> destination;
		
		CLParser CLPars;
		FILE_ File;

		char * workingPath;
		int actualSource;
		int actualTarget;
		bool getRegEx;
		bool readDirectoryFiles;
		static int IsDirectory(std::string path);
		MyGlobalClass(int argc, char* argv[]);
		//: CLPars(globInst), FIle(globInst);
};


New - initiation list added:
1
2
3
4
5
class MyGlobalClass{
public:

		MyGlobalClass(int argc, char* argv[]): CLPars(this), File(this){};
};


I compiled it. Thanks!


ne555: is it necessary to use initiation list? I think this should be possible to use it like SubClass newInstance(this) too.

Last edited on
Where does the C2819 occur?
1
2
3
4
foo::foo()
{ //at this point all the members are already constructed. Also the base class constructor was invoked
   bar asdf(42); //this variable is local to the function
} //it dies here 
To all:
see update - previous post of mine - compile completed.

ne555
I did it like this, but this did not helped. I did this in cpp file. The initiation list helped when I add it to header.
I was showing you how what you did was incorrect and why the initialization list was necessary
You had not only scoping, but masking too. And then some.

You had:
1
2
3
4
5
6
7
8
9
class MyGlobalClass {
  CLParser CLPars;

  MyGlobalClass(int argc, char* argv[])
  : CLPars() // error: no suitable constructor
  {
    CLParser * CLPars = this; // error1: local variable masks this->CLPars; error2: different pointer types
  }
};
I make notes to my personal needs, so hopfully I will not repeat that mistake.

How can I get rid of the warning:
warning C4355: 'this' : used in base member initializer list
is there something like directive
#define _CRT_SECURE_NO_WARNINGS
This one does not disable this warning.
Last edited on
I have yet one more similar problem with the 'no appropriate default constructor' error. In MyGlobalClass constructor I am having two vectors: sources and destination. I tried to create constructor for SRC to pass pointer to MyGlobalClass. However if I create initiation list to MyGlobalClass constructor initiating sources it prints error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class MyGlobalClass{
public:
		// Source *source;
		
	std::vector<SRC> sources;
	std::vector<TARGET> destination;

		CLParser CLPars;
		FILE_ File;
		
		__BITMAP BMP; // just test
		// SRC Src;

		char * workingPath;
		int actualSource;
		int actualTarget;
		bool getRegEx;
		bool readDirectoryFiles;
		static int IsDirectory(std::string path);
		MyGlobalClass(int argc, char* argv[]): 
			CLPars(this), File(this), BMP(this), sources(this) {
/* HERE PRINTS ERROR: error C2664: 'std::vector<_Ty>::vector(const std::allocator<_Ty> &)' : cannot convert parameter 1 from 'MyGlobalClass *const ' to 'const std::allocator<_Ty> &'
          with
          [
              _Ty=SRC
          ]
          Reason: cannot convert from 'MyGlobalClass *const ' to 'const std::allocator<_Ty>'
          with
          [
              _Ty=SRC
          ]
          No constructor could take the source type, or constructor overload resolution was ambiguous
  parse3.cpp
s:\temp\c++\algoritmy\processing_args\parse4\parse2\main.h(16): error C2512: '__BITMAP' : no appropriate default constructor available
s:\temp\c++\algoritmy\processing_args\parse4\parse2\global.h(21): error C2664: 'std::vector<_Ty>::vector(const std::allocator<_Ty> &)' : cannot convert parameter 1 from 'MyGlobalClass *const ' to 'const std::allocator<_Ty> &'
          with
          [
              _Ty=SRC
          ]
          Reason: cannot convert from 'MyGlobalClass *const ' to 'const std::allocator<_Ty>'
          with
          [
              _Ty=SRC
          ]
          No constructor could take the source type, or constructor overload resolution was ambiguous
  global.cpp
main.h(16): error C2512: '__BITMAP' : no appropriate default constructor available
global.h(21): error C2664: 'std::vector<_Ty>::vector(const std::allocator<_Ty> &)' : cannot convert parameter 1 from 'MyGlobalClass *const ' to 'const std::allocator<_Ty> &'
          with
          [
              _Ty=SRC
          ]
          Reason: cannot convert from 'MyGlobalClass *const ' to 'const std::allocator<_Ty>'
          with
          [
              _Ty=SRC
          ]
          No constructor could take the source type, or constructor overload resolution was ambiguous
  fnc.cpp
Build has been canceled. */



		CLPars.ParseArgs(argc, argv);
		};
};


main.h defines SRC
1
2
3
4
5
6
7
class SRC {  
private:
	MyGlobalClass * PGloablInstance;
public:
	SRC( MyGlobalClass * globInst ):PGloablInstance(globInst){}; // HERE PRINTS ERROR: '__BITMAP' : no appropriate default constructor available
};
__BITMAP bitmap;


Any idea how to solve this?

Yet the bitmap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
class __BITMAP {
    private:
	   int result;
	   MyGlobalClass * PGloablInstance;
	   WCHAR * pFilename;
	   Gdiplus::GpBitmap* pBitmap;
	   HBITMAP HBitmap;
	   void printError(char * error, int result );
	public:
		__BITMAP::__BITMAP( MyGlobalClass * globInst );
		//__BITMAP( MyGlobalClass * globInst ):PGloablInstance(globInst){};
		bool saveFile(Gdiplus::GpBitmap *pBitmap, const WCHAR *pFilename);
};
Last edited on
'sources' is a vector
to construct a vector, call the vector constructor
He does attempt to call a vector constructor. The problem is that he effectively calls:
1
2
MyGlobalClass * foo = ...
std::vector<SRC> sources( foo );

Now, that is not a copy/move contructor call, so other alternatives are:
1
2
explicit vector (const allocator_type& alloc = allocator_type());
explicit vector (size_type n);

Pointer is not a size_type and it is not an allocator either.

What he might intend to call is the second form of the fill constructor:
1
2
vector (size_type n, const value_type& val,
        const allocator_type& alloc = allocator_type());

Pointer is not a value_type either, so to be explicit:
1
2
MyGlobalClass * foo = ...
std::vector<SRC> sources( 1, SRC(foo) );

However, a design where an object A has a list of objects of same type that contains object A itself is dubious.
Keskiverto:
"However, a design where an object A has a list of objects of same type that contains object A itself is dubious. "
Not at all. This is pointer to object, not object. The constructor receives pointer to the parent instance.
Last edited on
I did not realized that this is call of vector constructor. I intended to pass pointer to this to the object of type SRC. So when I add new SRC object into vector, so the SRC constructor should be called. Will you help me to do this here or should I create new question about vector?
Last edited on
My previous comment contains the answer in disguise.
Pages: 12