I have a class Bar:
class Bar
{
public:
Bar(void);
~Bar(void);
};
And a class Foo that gets a reference to Bar object as a constructor parameter and needs to save it in a private member bar_ :
class Foo
{
private:
Bar& bar_;
public:
Foo(Bar& bar) : bar_(bar) {}
~Foo(void) {}
};
This doesn’t compile :
overloaded member function not found in ‘Foo’
missing type specifier – int assumed. Note: C++ does not support default-int
Now i suspect couple of things that i need to assure, the second error is for Bar& bar_; declaration in Foo. Do i need to use an explicit constructor when declaring bar_ ?
I am interested in learning how the compiler works regarding this matter, so a detailed explanation would be highly appreciated.
Thanks.
EDIT
Okay I am posting a new code, since apparently there was nothing wrong with my code.
Parser.h:
#pragma once
class Parser
{
private:
std::istream& inputStream_;
Analyzer& analyzer_;
public:
Parser(std::istream &inputStream, Analyzer& analyzer);
~Parser(void);
};
Parser.cpp :
#include "stdafx.h"
#include "Parser.h"
#include "Analyzer.h"
Parser::Parser(std::istream &inputStream, Analyzer& analyzer ) : inputStream_(inputStream), analyzer_(analyzer) {}
Parser::~Parser(void) {}
Analyzer.h :
#pragma once
class Analyzer
{
public:
Analyzer(void);
~Analyzer(void);
};
The code snippet you provided does indeed compile. I can tell you a little bit about these errors though.
The first error message happens when you compile a method that has the same name but a different signature from methods declared with the same name in your class. For example, if you had:
The compiler will issue an error about not finding an overloaded member function. The compiler thinks
foois overloaded, becausefoo(void)is different fromfoo(int).The second error happens when you define a variable or function without a type. This is usually not the actual problem, but a consequence of some other problem. For example, if your code tried to use a class before it was declared, like:
You would get the second error about missing type specifier, but it is talking about
Bon the first line.