Whats the problem, me or the ide? (constructors)

Not sure if i'm getting crazy or visual studio needs a vacation.

I've 2 classes:
1
2
3
4
5
6
7
8
9
10
11
12
13

class a
   {
   public:
      a(b::vartype p)
   }


class b
   {
   public:
      typedef int vartype;
   }



But when in class b i try to build an instance of a (don't worry, the header is included)

1
2
...
a(5);


Visual studio tells me it cant cast int (or vartype) to &a, only recognising the default constructor… what's going on?
Hi,

Not sure what exactly is in what files, but class a ctr has a b object, has the compiler seen b at this point? You may need a forward declaration of b before class a:

1
2
3
4
5
6
class b; // forward declaration
class a
   {
   public:
      a(b::vartype p)
   }


can you post your actual code that we can compile?
here's an idea, create a minimal testcase that contains evertything that's needed to reproduce your issue and post that.
don't expect us to fill the blanks with your same mistakes.
TheIdeasMan it does, as i said the header is included

function_scope.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma once
#include "interpreter.h"
#include "instruction.h"

class function_scope
	{
	private:
		interpreter::instructions_vector* instructions;

	public:
		function_scope(interpreter::instructions_vector* instructions);
		~function_scope();
	};


function_scope.c
1
2
3
4
5
6
7
#include "function_scope.h"

function_scope::function_scope(interpreter::instructions_vector* instructions) 
	: instructions(instructions)
	{}

function_scope::~function_scope(){}


interpreter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#pragma once
#include "instruction.h"
#include "function_scope.h"
#include <vector>
#include <string>

class interpreter
	{
	public:
		typedef std::vector<instruction*> instructions_vector;

	private:
		std::map<std::string, instructions_vector*> functions;

	public:
		void exec();

		void function_call(std::string name);

		interpreter();
		~interpreter();
	};


interpreter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "interpreter.h"
void interpreter::function_call(std::string name)
	{
	if (functions.find(name) == functions.end())
		{
		std::cerr << "ERROR: function " << name << " not found." << std::endl;
		return;
		}

	function_scope f(functions[name]);
	f.exec();
	}

interpreter::interpreter(){}
interpreter::~interpreter(){}


Error:
Error C2664 'function_scope::function_scope(const function_scope &)': cannot convert argument 1 from 'std::vector<instruction *,std::allocator<_Ty>> *' to 'const function_scope &'

As if only default copy constructor existed
Last edited on
SOLVED
with a ton of forward declaration; although i'd apreciate if someone explained me why they are necessary even when the .h file including that same class is already included, thanks
Hi,

So that is a circular include: function_scope has an interpreter , and interpreter has a function_scope

So this is fixed with a forward declaration as well:

1
2
class interpreter;
class function_scope {/* definition*/}


1
2
class function_scope;
class interpreter {/* definition*/}





Last edited on
Hi, didn't actually compile your code, but I believe your problem (at least, one of them) is similar to what TheIdeasMan already said. (EdiT: I had my window open for a while and didn't see that TheIdeasMan already replied again about 15 minutes before me :))

You have interpreter.h #including function_scope.h, but then you have function_scope.h including interpreter.h. This is circular logic. What happens if you were to move your #include "function_scope.h" to the beginnering of interpreter.c?

Also, you need to #include <map> in interpreter.h.

PS: In C++, it might confuse some IDEs (and other programmers) if you name C++ files with .c extension. Usually C++ files are .cpp or .cc or .cxx. I most often see .cpp.
Last edited on
On the file extensions: I like *.hpp rather than *.h for C++ header files. It's just a convention, but *.h could mean a C header file.



gccmanual wrote:
3.3 Compiling C++ Programs

C++ source files conventionally use one of the suffixes ‘.C’, ‘.cc’, ‘.cpp’, ‘.CPP’, ‘.c++’, ‘.cp’, or ‘.cxx’; C++ header files often use ‘.hh’, ‘.hpp’, ‘.H’, or (for shared template code) ‘.tcc’; and preprocessed C++ files use the suffix ‘.ii’. GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc).
the files are actually .cpp and .h as created by default from visual studio

Thanks for the answers!
Last edited on
Topic archived. No new replies allowed.