I have a code,
class foo : public bar
{
public:
foo(){};
~foo(){};
};
class wu
{
public:
wu(const bar& Bar ) :
m_bar(Bar)
{};
~wu(){};
private:
bar m_bar;
};
int main()
{
foo tmpFoo;
wu tmpWu(tmpFoo);
}
Now my problem is, the code above will not compile and the error message is "error: variable wu tmpWu has initializer but incomplete type".
Does it mean, I have to cast the tmpFoo object to bar class?
Please advice.
Thanks.
You must use the syntax m_bar(Bar) instead of m_bar = Bar in the wu class constructor. Also, remove the braces from the tmpFoo variable declaration, otherwise you will be declaring a function that returns a foo object and receives no arguments.
After your edit: I tried that code, and the problem it gave was that the bar class was undefined. In your case, the compiler gave an "incomplete type" error; that means that somewhere in an included file (or in the same file), the class bar is declared this way:
class bar;
but it is never defined its contents.
adding
class bar {};
your code works for me. Am I missing something?
Related
header.h
class Foo{
public:
void fooFunction(std::map <std::string, Bar> &);
}
class Bar{
public:
void barFunction(std::map <std::string, Foo> &, Foo &);
}
When I try to compile this, I get an error saying Bar is not declared in the scope of fooFunction, and when I switch the order of the declarations, I get an error saying Foo is not in the scope of barFunction.
How can I make them be in the scope of each other? If I need multiple header files, how should I set that up in my makefile and #include s?
You can simply forward-declare the other class in the header that requires it:
class Bar;
class Foo
{
public:
void fooFunction(std::map <std::string, Bar>&);
};
class Bar
{
public:
void barFunction(std::map<std::string, Foo>&, Foo &);
};
This works provided that Foo does not use Bar by value anywhere until after Bar has been defined. That means storing it as a member, passing as parameter, returning value of that type. In this case you're fine, since the Foo parameters are passed by reference.
Note that even though your std::map stores type Bar by value, this is still okay because it's a template and is not resolved until you use it, at which time Bar will be defined.
I'm getting this strange problem which I don't know why happens. The first and second of the following code snippets compile, while the third does not:
Compiles:
class Foo {
public:
Foo() { Bar(); }
private:
class Bar {};
};
Compiles:
class Foo {
class Bar {}; // Or only forward declare here and define later
public:
Foo(Bar) {}
}
Does not compile:
class Foo {
public:
Foo(Bar) {}
private:
class Bar {};
};
What makes the third fail to compile while the first can?
Normally, in C++, you can only reference declarations that were previously made in the translation unit. However, within a class definition, the definition of member functions are allowed to reference declarations which are made later in the class. Basically, the compiler restructures your in-class definitions so that they work as though they were written just after the class.
But this is only true of the function definitions. The declaration of the function (including parameter types) isn't allowed to do this. They can only reference declarations that have already been made in file order.
So you can do this:
class Test
{
public:
void Func(int x) {Inner foo;}
private:
class Inner {};
};
But not this:
class Test
{
public:
void Func(Inner x) {}
private:
class Inner {};
};
First example does not expose anything about private Bar to the outside, while third does.
Third example is pretty much saying, that there exist some class Foo, which has constructor with single argument of type Bar. But Bar is unknown to the outside. Imagine calling such constructor.
Foo f{Foo::Bar{}};
Will result probably in something like Foo::Bar is inaccessible.
I've been working on a project and have quite a few classes, a few of which look like this:
class A
{
// stuff;
};
class B
{
A& test;
public:
B(A& _test) :
test(_test)
{};
void doStuff();
};
class C
{
A foo;
B bar(foo);
void exp()
{
bar.doStuff();
}
};
This ends up breaking in class C when C::foo is not a type name. In my bigger project, where everything is broken up into their separate .cpp and .h files, I don't see that error if I #include"C.h" in B.h, but there is still an error in C.cpp where bar is completely unrecognized by my compiler (Visual Studio 2013). There error persists even if A& is an A* instead (changing the code from references to pointers where necessary, of course). Does anyone have any tips to what is going on here?
This line of code:
B bar(foo);
Attempts to declare a member function named bar which returns a B and takes an argument of type foo. However, in your code, foo is not a type - it's a variable. I'm guessing you meant to initialize bar instead:
B bar{foo}; // non-static data member initializers must be done with {}S
or just write out the constructor:
C()
: bar(foo)
{ }
Additionally, this constructor assigns your reference test to the temporary _test:
B(A _test) :
test(_test)
{ }
You want to take _test by reference:
B(A& _test) : test(_test) { }
I have a class defined as follows:
class Foo {
private:
boolean feature;
public:
Foo(boolean feature) : feature(feature) {}
// ...
};
I'm trying to construct an instance, as a private property of another class:
class Bar {
private:
Foo foo(true);
// ...
};
This doesn't work. I get expected identifier before numeric constant on the line with the declaration. When I remove the parameter from Foo's constructor definition simply and ask for a Foo foo;, it works.
Why?
How do I define and declare an instance of Foo that takes a boolean parameter?
You can't use that initialisation syntax in a class member declaration; you can only initialise members with {} or =. The following should work (assuming support for C++11 or later):
Foo foo{true};
Foo foo = Foo(true);
The pre-C++11 way to do this is:
class Bar {
public:
Bar() : foo(true){} //initialization
private:
Foo foo; //no parameter
};
Bonus:
class Bar {
private:
Foo foo(); //<- This is a function declaration for a function
//named foo that takes no parameters returning a Foo.
//There is no Foo object declared here!
};
I am working on a project and I am facing a little bit strange code which I cannot understand why and how can this happen !
I have a class Foo and Baz, and Foo has a non-static method that is called from Baz class without instantiating Foo:
class Foo {
public:
void qux(int a, int b);
};
class Baz {
public:
void bar(void);
};
void Baz::bar(void){
Foo::qux(2,3); // This should not happen as qux is not a static method !!
}
The only way that would work is if Baz was derived from Foo at some level.
Or, of course, Foo bears a different meaning in that scope (via a using, typedef, define or other).
If neither apply, your compiler is seriously broken.
This can happen in case of Baz is inherited from Foo.
In this case you can call method of the base class directly in the form that you mentioned:
void Baz::bar(void){
Foo::qux(2,3);
}