During refactoring of a rather large code-base my compiler came up with a great way to misunderstand me. This is a minimal example of what I am talking about:
#include <iostream>
class Foo {
public:
virtual int get() = 0;
template <typename T> int get(int i) { return 4 + i; }
};
class Bar : public Foo {
public:
virtual int get() { return 3; }
};
int main(int argv, char **argc) {
Bar b;
std::cout << b.get<char>(7) << std::endl;
return 0;
}
Clang 3.6, gcc 4.7, gcc 4.8 and gcc 4.9 all tokenize the "b.get(7)" as a comparison operator between "b.get" and "char".
template-test.cpp: In function ‘int main(int, char**)’:
template-test.cpp:16:17: error: invalid use of non-static member function
std::cout << b.get<char>(7) << std::endl;
^
template-test.cpp:16:21: error: expected primary-expression before ‘char’
std::cout << b.get<char>(7) << std::endl;
^
(This is gcc 4.9, the others say something similar)
Is this supposed to work?
The work-around I found was to declare the templated "get" in both the base and the derived class.
The name get in the derived class hides the name get in the base class. Hence, the function template get() is not found when performing name lookup, and the compiler can only interpret those tokens the way you've seen.
You can use a using declaration in your Bar class to fix that:
class Bar : public Foo {
public:
using Foo::get;
// ^^^^^^^^^^^^^^^
virtual int get() { return 3; }
};
Here is a live demo on Coliru.
If you cannot modify the definition of Bar because it is not under your control, I guess you could qualify the call to get():
std::cout << f.Foo::get<char>(7) << std::endl; // get() template is found now.
See here for a live demo. Another option is to perform the call through a pointer or reference to Foo:
Bar b;
Foo& f = b;
std::cout << f.get<char>(7) << std::endl; // get() template is found now.
Once again, live example.
Related
From here: Difference between 'struct' and 'typedef struct' in C++?, I found I need class identifier if there is name collision (for example if class name is the same as function name):
#include <iostream>
using namespace std;
class foo
{
public:
foo() {}
operator char const *() const
{
return "class";
}
};
char const *foo()
{
return "function\n";
}
int main()
{
char const *p;
p = class foo(); //this gets error
cout << p << '\n';
return 0;
}
output:
error: expected primary-expression before ‘class’
p = class foo();
What is primary expression here and how can I identify the class instead of the function? I would like it to print class instead of function. How to do so?
One of the possible solutions:
using bar = class foo;
p = bar();
int main()
{
char const *p;
struct foo f;
p = static_cast<char const*>(f);
cout << p << '\n';
return 0;
}
By the way, the answer you link mentions that one can use typedef class foo foo; to trigger a compiler error for a function of same name. Havning a class and a function of same name isn't something desirable, but rather you need to workaround a bit the fact that the language allows it. And don't miss the last paragraph:
I can't imagine why anyone would ever want to hide a class name with a
function or object name in the same scope as the class. The hiding
rules in C were a mistake, and they should not have been extended to
classes in C++. Indeed, you can correct the mistake, but it requires
extra programming discipline and effort that should not be necessary.
If you are in control of either the function or the class you should definitely rename it or place it inside a namespace.
I found two solutions that are accepted by both g++ and clang. I do not know, however, if they are standard C++.
Uniform initialization
cout << (class foo){} << "\n";
Using a helper
template <typename T, typename ...Args>
T ctor(Args&& ...args) {
return T{ std::forward<Args>(args) ... };
}
// ...
cout << ctor<class foo>() << "\n";
I have a class with two constructors, one that takes no arguments and one that takes one argument.
Creating objects using the constructor that takes one argument works as expected. However, if I create objects using the constructor that takes no arguments, I get an error.
For instance, if I compile this code (using g++ 4.0.1)...
class Foo
{
public:
Foo() {};
Foo(int a) {};
void bar() {};
};
int main()
{
// this works...
Foo foo1(1);
foo1.bar();
// this does not...
Foo foo2();
foo2.bar();
return 0;
}
... I get the following error:
nonclass.cpp: In function ‘int main(int, const char**)’:
nonclass.cpp:17: error: request for member ‘bar’ in ‘foo2’, which is of non-class type ‘Foo ()()’
Why is this, and how do I make it work?
Foo foo2();
change to
Foo foo2;
You get the error because compiler thinks of
Foo foo2()
as of function declaration with name 'foo2' and the return type 'Foo'.
But in that case If we change to Foo foo2 , the compiler might show the error " call of overloaded ‘Foo()’ is ambiguous".
Just for the record..
It is actually not a solution to your code, but I had the same error message when incorrectly accessing the method of a class instance pointed to by myPointerToClass, e.g.
MyClass* myPointerToClass = new MyClass();
myPointerToClass.aMethodOfThatClass();
where
myPointerToClass->aMethodOfThatClass();
would obviously be correct.
Parenthesis is not required to instantiate a class object when you don't intend to use a parameterised constructor.
Just use Foo foo2;
It will work.
Adding to the knowledge base, I got the same error for
if(class_iter->num == *int_iter)
Even though the IDE gave me the correct members for class_iter. Obviously, the problem is that "anything"::iterator doesn't have a member called num so I need to dereference it. Which doesn't work like this:
if(*class_iter->num == *int_iter)
...apparently. I eventually solved it with this:
if((*class_iter)->num == *int_iter)
I hope this helps someone who runs across this question the way I did.
I was having a similar error, it seems that the compiler misunderstand the call to the constructor without arguments. I made it work by removing the parenthesis from the variable declaration, in your code something like this:
class Foo
{
public:
Foo() {};
Foo(int a) {};
void bar() {};
};
int main()
{
// this works...
Foo foo1(1);
foo1.bar();
// this does not...
Foo foo2; // Without "()"
foo2.bar();
return 0;
}
I ran into a case where I got that error message and had
Foo foo(Bar());
and was basically trying to pass in a temporary Bar object to the Foo constructor. Turns out the compiler was translating this to
Foo foo(Bar(*)());
that is, a function declaration whose name is foo that returns a Foo that takes in an argument -- a function pointer returning a Bar with 0 arguments. When passing in temporaries like this, better to use Bar{} instead of Bar() to eliminate ambiguity.
If you want to declare a new substance with no parameter (knowing that the object have default parameters) don't write
type substance1();
but
type substance;
Certainly a corner case for this error, but I received it in a different situation, when attempting to overload the assignment operator=. It was a bit cryptic IMO (from g++ 8.1.1).
#include <cstdint>
enum DataType
{
DT_INT32,
DT_FLOAT
};
struct PrimitiveData
{
union MyData
{
int32_t i;
float f;
} data;
enum DataType dt;
template<typename T>
void operator=(T data)
{
switch(dt)
{
case DT_INT32:
{
data.i = data;
break;
}
case DT_FLOAT:
{
data.f = data;
break;
}
default:
{
break;
}
}
}
};
int main()
{
struct PrimitiveData pd;
pd.dt = DT_FLOAT;
pd = 3.4f;
return 0;
}
I received 2 "identical" errors
error: request for member ‘i’ [and 'f'] in ‘data’, which is of non-class type ‘float’
(The equivalent error for clang is:
error: member reference base type 'float' is not a structure or union)
for the lines data.i = data; and data.f = data;. Turns out the compiler was confusing local variable name 'data' and my member variable data. When I changed this to void operator=(T newData) and data.i = newData;, data.f = newData;, the error went away.
#MykolaGolubyev has already given wonderful explanation. I was looking for a solution to do somthing like this MyClass obj ( MyAnotherClass() ) but the compiler was interpreting it as a function declaration.
C++11 has braced-init-list. Using this we can do something like this
Temp t{String()};
However, this:
Temp t(String());
throws compilation error as it considers t as of type Temp(String (*)()).
#include <iostream>
class String {
public:
String(const char* str): ptr(str)
{
std::cout << "Constructor: " << str << std::endl;
}
String(void): ptr(nullptr)
{
std::cout << "Constructor" << std::endl;
}
virtual ~String(void)
{
std::cout << "Destructor" << std::endl;
}
private:
const char *ptr;
};
class Temp {
public:
Temp(String in): str(in)
{
std::cout << "Temp Constructor" << std::endl;
}
Temp(): str(String("hello"))
{
std::cout << "Temp Constructor: 2" << std::endl;
}
virtual ~Temp(void)
{
std::cout << "Temp Destructor" << std::endl;
}
virtual String get_str()
{
return str;
}
private:
String str;
};
int main(void)
{
Temp t{String()}; // Compiles Success!
// Temp t(String()); // Doesn't compile. Considers "t" as of type: Temp(String (*)())
t.get_str(); // dummy statement just to check if we are able to access the member
return 0;
}
Goal
My objective is to call StaticLibrary::func() from the property (unnamed class) on Environment using the dot syntax.
For example:
env.bar.func();
I have been able to achieve static_cast<StaticLibrary>(env.bar).func();, which is close, but the syntax is still too cumbersome.
Question
Can the static cast be inferred, or can I overload some operator to get the desired syntax?
NOTE: I have a constraint that I cannot put StaticLibrary directly as a public member of the Environment class (object, reference or pointer).
Error
I currently get the error (which I understand, but pasted here for completeness):
unnamedDotSyntax.cpp: In function ‘int main()’:
unnamedDotSyntax.cpp:48:13: error: ‘class Environment::<anonymous>’ has no member named ‘func’
env.bar.func();
^
Code
The example below is the most distilled version of the code I can offer.
#include <iostream>
class StaticLibrary {
public:
int func (void) {
std::cout << "called " << __FUNCTION__ << std::endl;
}
};
class Environment {
public:
Environment (void) {
bar.sl = &sl;
}
inline
int foo (void) {
std::cout << "called " << __FUNCTION__ << std::endl;
}
class {
friend Environment;
public:
operator StaticLibrary & (void) {
return *sl;
}
private:
StaticLibrary * sl;
} bar;
private:
StaticLibrary sl;
};
int main (void) {
Environment env;
env.foo();
// Works
StaticLibrary sl = env.bar;
sl.func();
// Works, but the syntax is too cumbersome. Can the static cast be inferred somehow?
static_cast<StaticLibrary>(env.bar).func();
// unnamedDotSyntax.cpp:48:13: error: ‘class Environment::<anonymous>’ has no member named ‘func’
// env.bar.func();
env.bar.func();
}
NOTE: This must be GCC compatible not Microsoft VC++
For the nested class there is no way around replicating the interface of StaticLibrary, because the member access operator (.) does not apply any conversions. So to call func() on bar you need to have a member function func() in bar. It does not suffice if bar converts to something that has a member function func() (because this could be ambiguous).
That is to say, you could wrap the interface of StaticLibrary inside bar by having a delegating member function int func() { return sl.func(); } or you make bar a public data member of type StaticLibrary (which was forbidden by your constraint).
Here I gave the nested class a name because it makes the errors more readable and I store a reference rather than a pointer because I like value semantics.
#include <iostream>
class StaticLibrary {
public:
int func() {
std::cout << "called " << __FUNCTION__ << std::endl;
return 0;
}
};
class Environment {
private:
StaticLibrary sl;
public:
class Bar {
friend Environment;
StaticLibrary& sl;
public:
explicit Bar(StaticLibrary& _sl) : sl(_sl) {};
operator StaticLibrary& () { return sl; }
int func() { return sl.func(); }
} bar;
Environment() : sl{}, bar{sl} {};
int foo() {
std::cout << "called " << __FUNCTION__ << std::endl;
return 0;
}
};
int main (void) {
Environment env;
env.foo();
// Works
StaticLibrary sl = env.bar;
sl.func();
// Works, but the syntax is too cumbersome. Can the static cast be inferred somehow?
static_cast<StaticLibrary>(env.bar).func();
// unnamedDotSyntax.cpp:48:13: error: ‘class Environment::<anonymous>’ has no member named ‘func’
// env.bar.func();
env.bar.func();
}
This code works:
struct Defs
{
static const int a = 1;
int b{};
void g() {}
};
struct Bob : Defs
{
void f()
{
cout << a << "\n";
cout << b << "\n";
g();
}
};
int main()
{
Bob b;
b.f();
}
But this code doesn't:
struct Defs
{
static const int a = 1;
int b{};
void g() {}
};
template<class D>
struct Bob : D
{
void f()
{
cout << a << "\n";
cout << b << "\n";
g();
}
};
int main()
{
Bob<Defs> b;
b.f();
}
Errors:
prog.cpp: In member function 'void Bob<D>::f()':
prog.cpp:16:11: error: 'a' was not declared in this scope
cout << a << "\n";
^
prog.cpp:17:11: error: 'b' was not declared in this scope
cout << b << "\n";
^
prog.cpp:18:5: error: there are no arguments to 'g' that depend on a template parameter, so a declaration of 'g' must be available [-fpermissive]
g();
^
prog.cpp:18:5: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
But if I do the following, it works:
template<class D>
struct Bob : D
{
void f()
{
cout << D::a << "\n";
cout << D::b << "\n";
D::g();
}
};
Is it possible to get a class to use the members of a base class provided as a template parameter, without qualifying them? The reason I ask is because doing so would allow me to refactor some code without a LOT of changes.
It can be assumed the type used as the template parameter has all those members, otherwise a compile failure is acceptable.
Introduction
You get the error because the base-class is dependent on a template-parameter, which isn't too surprising since the base-class is the direct use of the template-parameter.
The error diagnostic comes from the fact that different template-parameters could yield significantly different behavior inside the class; what if the passed in template-parameter doesn't have a certain member; are we then to look up something in the global scope?
Where and why do I have to put the “template” and “typename” keywords?
Explicitly state that you would like to access a member of this
You are saying that you would like to access members of the base-class without qualifying them, and if I were to take you literally on this I would say that you could use this->member-name — but I doubt that this is what you are after given what you wrote about refactoring.
struct A {
int m;
};
template<class T>
struct B : T {
void func () {
this->m = 1;
}
};
int main () {
B<A> {}.func ();
}
Bring in names from the dependent base-class
Another alternative is to explicitly state that you would like certain names from your base-class to be available directly in that which derives from it— using using, as in the below:
template<class T>
struct B : T {
using T::m;
void func () {
m = 1;
}
};
The above can be read as; "dear compiler, wherever I'm referring to m I would like you to use the one in T".
But I want to hack the shit out of this problem; how!?
Alright, introduce a non-dependent base and have that introduce references to the data that you really want. This will work if you know what names that you will want to pull in for every T.
You can even extend this hack to automatically have it deduce the type of those members, but that is far away from the scope of the question.
#include <iostream>
struct A {
int n;
int m;
void print () {
std::cout << m << std::endl;
}
};
struct Hack {
template<class T>
Hack (T* hck) : m (hck->m), n (hck->n) { }
int& m;
int& n;
};
template<class T>
struct B : T, Hack {
B () : Hack (static_cast<T*> (this)) { }
void func () {
m = 123;
}
};
int main () {
B<A> b;
b.func ();
b.print ();
}
You can find a running example here. Word of warning; I would personally never do this, but as you can see it is possible to do what you ask through a little bit of indirection.
You can add:
using D::a;
using D::b;
using D::g;
to Bob to fix your scoping issue.
Here is a comprehensive overview of this problem. Honestly, it's a corner of C++ that shouldn't exist, but, no language is perfect =P
I have a class with two constructors, one that takes no arguments and one that takes one argument.
Creating objects using the constructor that takes one argument works as expected. However, if I create objects using the constructor that takes no arguments, I get an error.
For instance, if I compile this code (using g++ 4.0.1)...
class Foo
{
public:
Foo() {};
Foo(int a) {};
void bar() {};
};
int main()
{
// this works...
Foo foo1(1);
foo1.bar();
// this does not...
Foo foo2();
foo2.bar();
return 0;
}
... I get the following error:
nonclass.cpp: In function ‘int main(int, const char**)’:
nonclass.cpp:17: error: request for member ‘bar’ in ‘foo2’, which is of non-class type ‘Foo ()()’
Why is this, and how do I make it work?
Foo foo2();
change to
Foo foo2;
You get the error because compiler thinks of
Foo foo2()
as of function declaration with name 'foo2' and the return type 'Foo'.
But in that case If we change to Foo foo2 , the compiler might show the error " call of overloaded ‘Foo()’ is ambiguous".
Just for the record..
It is actually not a solution to your code, but I had the same error message when incorrectly accessing the method of a class instance pointed to by myPointerToClass, e.g.
MyClass* myPointerToClass = new MyClass();
myPointerToClass.aMethodOfThatClass();
where
myPointerToClass->aMethodOfThatClass();
would obviously be correct.
Parenthesis is not required to instantiate a class object when you don't intend to use a parameterised constructor.
Just use Foo foo2;
It will work.
Adding to the knowledge base, I got the same error for
if(class_iter->num == *int_iter)
Even though the IDE gave me the correct members for class_iter. Obviously, the problem is that "anything"::iterator doesn't have a member called num so I need to dereference it. Which doesn't work like this:
if(*class_iter->num == *int_iter)
...apparently. I eventually solved it with this:
if((*class_iter)->num == *int_iter)
I hope this helps someone who runs across this question the way I did.
I was having a similar error, it seems that the compiler misunderstand the call to the constructor without arguments. I made it work by removing the parenthesis from the variable declaration, in your code something like this:
class Foo
{
public:
Foo() {};
Foo(int a) {};
void bar() {};
};
int main()
{
// this works...
Foo foo1(1);
foo1.bar();
// this does not...
Foo foo2; // Without "()"
foo2.bar();
return 0;
}
I ran into a case where I got that error message and had
Foo foo(Bar());
and was basically trying to pass in a temporary Bar object to the Foo constructor. Turns out the compiler was translating this to
Foo foo(Bar(*)());
that is, a function declaration whose name is foo that returns a Foo that takes in an argument -- a function pointer returning a Bar with 0 arguments. When passing in temporaries like this, better to use Bar{} instead of Bar() to eliminate ambiguity.
If you want to declare a new substance with no parameter (knowing that the object have default parameters) don't write
type substance1();
but
type substance;
Certainly a corner case for this error, but I received it in a different situation, when attempting to overload the assignment operator=. It was a bit cryptic IMO (from g++ 8.1.1).
#include <cstdint>
enum DataType
{
DT_INT32,
DT_FLOAT
};
struct PrimitiveData
{
union MyData
{
int32_t i;
float f;
} data;
enum DataType dt;
template<typename T>
void operator=(T data)
{
switch(dt)
{
case DT_INT32:
{
data.i = data;
break;
}
case DT_FLOAT:
{
data.f = data;
break;
}
default:
{
break;
}
}
}
};
int main()
{
struct PrimitiveData pd;
pd.dt = DT_FLOAT;
pd = 3.4f;
return 0;
}
I received 2 "identical" errors
error: request for member ‘i’ [and 'f'] in ‘data’, which is of non-class type ‘float’
(The equivalent error for clang is:
error: member reference base type 'float' is not a structure or union)
for the lines data.i = data; and data.f = data;. Turns out the compiler was confusing local variable name 'data' and my member variable data. When I changed this to void operator=(T newData) and data.i = newData;, data.f = newData;, the error went away.
#MykolaGolubyev has already given wonderful explanation. I was looking for a solution to do somthing like this MyClass obj ( MyAnotherClass() ) but the compiler was interpreting it as a function declaration.
C++11 has braced-init-list. Using this we can do something like this
Temp t{String()};
However, this:
Temp t(String());
throws compilation error as it considers t as of type Temp(String (*)()).
#include <iostream>
class String {
public:
String(const char* str): ptr(str)
{
std::cout << "Constructor: " << str << std::endl;
}
String(void): ptr(nullptr)
{
std::cout << "Constructor" << std::endl;
}
virtual ~String(void)
{
std::cout << "Destructor" << std::endl;
}
private:
const char *ptr;
};
class Temp {
public:
Temp(String in): str(in)
{
std::cout << "Temp Constructor" << std::endl;
}
Temp(): str(String("hello"))
{
std::cout << "Temp Constructor: 2" << std::endl;
}
virtual ~Temp(void)
{
std::cout << "Temp Destructor" << std::endl;
}
virtual String get_str()
{
return str;
}
private:
String str;
};
int main(void)
{
Temp t{String()}; // Compiles Success!
// Temp t(String()); // Doesn't compile. Considers "t" as of type: Temp(String (*)())
t.get_str(); // dummy statement just to check if we are able to access the member
return 0;
}