Why won't my simple c++ code compile? I get undeclared identifier? - c++

Why won't my simple c++ code compile? get undeclared identifier errors.
I can't see the problem
Thanks a lot
int _tmain(int argc, _TCHAR* argv[])
{
StateMachine<States,Triggers> sm;
return 0;
}
enum States
{
New,
Complete
};
enum Triggers
{
CreateNew,
MoveToComplete
};
template <class TState, class TTrigger> class StateMachine
{
public:
StateMachine();
};

Either forward declare or move the _tmain to the bottom, also you've not provided an implementation for the constructor.

You have to forward-declare the StateMachine class, otherwise the compiler does not now how to handle that identifier as it hasn't been told to him what it actually is, yet, or at least, that it exists.
template<class TState, class TTrigger> class StateMachine;
int _tmain(int argc, _TCHAR* argv[])
{
StateMachine<States,Triggers> sm;
return 0;
}
// ...
template <class TState, class TTrigger> class StateMachine
{
public:
StateMachine();
};

The compiler reads the code from top to bottom - it can only understand things that have already been declared

Related

Instance of C++ template class as a member of another template class

Let's say I have following templated C++ class
#include <cstdint>
template <uint32_t NO_POINTS>
class A
{
public:
struct Point
{
float x;
float y;
};
A(const Point (&points)[NO_POINTS])
{
for (uint32_t point = 0; point < NO_POINTS; point++) {
table[point] = points[point];
}
}
private:
Point table[NO_POINTS];
};
and I would like to use an instance of this class as a private member of the following class:
#include "A.h"
template <uint32_t NO_LUT_POINTS>
class B
{
public:
B(A<NO_LUT_POINTS>::Point (&table)[NO_LUT_POINTS]) : lut(table){}
private:
A<NO_LUT_POINTS> lut;
};
#include "B.h"
int main(int argc, char** argv) {
B<4> foo({{1326.0, 25.0}, {1601.0, 30.0}, {1922.0, 35.0}, {2293.0, 40.0}});
return 0;
}
I have attempted to compile this code but the compiler reports following error
A<NO_LUT_POINTS>::Point is not a type. I don't understand what the reason for this error is. Can anybody explain to me why the compiler reports this error?
This is a common mistake with types nested in template classes. You need to add typename to tell the compiler that Point is a type.
...
public:
B(typename A<NO_LUT_POINTS>::Point const (&table)[NO_LUT_POINTS]) : lut(table){}
...
Beyond solving your problem, however, please notice that Point doesn't depend on the template parameters of A, so you should not nest it in that class. This would remove the necessity for adding typename.

pass pointer to template member of template class

I'm searching for a way to have a function of my template class which accepts as a parameter a member of the template (it could be either one of its arguments or functions).
model.h
template <class T>
class Model
{
public:
Model(){}
void relate(int T::*, int ){}
};
main.cpp
#include "model.h"
class element
{
public:
element();
char getA(){return this->a;}
int getB(){return this->b;}
char a;
int b;
};
int main(int argc, char *argv[]){
Model<element *> model;
return 0;
}
it gives the creating pointer to member of non-class type ‘element*’ error.
I agree with that since pointer must point to some defined type memory area.
My final goal is to tell my class that I want to relate a specific member of its template with some number which later will be used to order different members in a list which later will be displayed.
This wont be used with types such as Model<int> but only with other classes.
Your instantiate a Model<element*> hence this:
void relate(int T::*, int ){}
is (just hypotheically):
void relate(int element*::*, int) {}
Its a "pointer to member of a pointer type". If you instantiate a Model<element> your code works fine. Alternatively you can use std::remove_pointer<T>.
EDIT:
suddenly I'm figuring out... I used a poiter as a template...
but the game changes now...
int main(int argc, char *argv[]){
Model<element> model;
element e;
model.relate(&element::getA(), 0); // <- call to non static-member error
model.relate(&element::getB(), 0); // <- call to non static-member error
model.relate(&element::a, 0); // <- type mismatch error
model.relate(&element::b, 0); // <- no error
}

How to check if a function is actually defined or not in C++ class

I want to determine if a function definition actually exist in a class and call that function only if it exists (not just declared). Idea is function declaration will always be present in the class header file but it's implementation (function definition) may or may not be included in the compilation based on the some flags. Catch is that I can not use compiler flags in the source cpp files. Below is what I want to achieve:
class Base {
public:
void feature1(int x); // Definition always present
void feature2(int y); // Definition always present
void feature3(int z); // Definition may (or may not) be present in another cpp file / library
};
int main(int argc, char *argv[]) {
Base a1;
if (a1.feature3 is available) { // pseudo code
a1.feature3(5);
} else {
printf("feature3 is not available\n");
}
return 0;
}
Can someone please help with a possible solution.
If one method is not defined (but only declare) and you try reference it, linker won't be able to link. Maybe you could try a different approach to the problem (but depends on the requirements that you have), you could try using polymorphism like so:
class Base
{
public:
virtual void feature3(){throw NotImplementException()}
}
class DerivedWithFeature3 : public Base
{
public:
void feature3(){/*do something*/}
}
int main(int argc, char *argv[]) {
Base a1;
try
{
a1.feature3(5);
}catch(NotImplementException&)
{
printf("feature3 is not available\n");
}
return 0;
}
The weak symbol should be able to do this for you:
class Base {
...
...
void feature3(int z) __attribute__((weak));
}
int main(int argc, char *argv[]) {
Base a1;
if (a1.feature3) {
a1.feature3(5);
} else {
printf("feature3 is not available\n");
}
return 0;
}

Getting function pointer of private member function in C++

Is there a way to get function pointer for a member function that is private inside a class
class A
{
public:
void callMe()
{
cout<<__FUNCTION__<<endl;
}
private:
void fooMem()
{
cout<<__FUNCTION__<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
auto fp = &A::fooMem;
return 0;
}
Compiling this in vs 2012 c++ compiler causes below error
error C2248: 'A::fooMem' : cannot access private member declared in class 'A'
see declaration of 'A::fooMem'
I looked into a amazing solution to a similar problem (though I am not very clear how this actually works, if someone can explain that would be great too), Here I want the address of the member not to invoke it.
The reason I am asking for the address is I'll be patching this function with a different implementation.
The class as such is not modifiable, But I can inherit if that can help achieve this.
Since you cannot modify the original class, it would be much easier to simply inherit the class and create a duplicate of the function in a public memberspace.
I tried this out and it works as expected, at least with g++:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
class A
{
public:
void aFunction()
{
cout<<"This is aFunction"<<endl;
}
private:
void anotherFunction()
{
cout<<"This is anotherFunction"<<endl;
}
};
class B: public A
{
public:
void anotherFunction()
{
cout<<"This is also anotherFunction, but it's accessible!"<<endl;
}
};
int main(int argc, char* argv[])
{
A firstClass;
B coach;
firstClass.aFunction();
coach.anotherFunction();
return 0;
}
When I run this code I get the following output:
$ ./a.out
This is aFunction
This is also anotherFunction, but it's accessible!
Proving the compiler understood which version of anotherFunction to use.

Error while inheriting std::vector

I am trying to define an inherited class from std::vector class. Below is the code
template<class t>
class Vector:vector<t>
{
public:
using vector<t>::vector;
};
int main(int argc, char *argv[])
{
Vector<int> v;
return 0;
}
And I am getting error like this:
"error: 'std::vector<t, std::allocator<_CharT> >::vector' names constructor"
So basically I would like to know why my program is failing and what all procedures to be overloaded in the my inherited class from parent std::vector class.
Thanks in advance.
I tried:
#include <vector>
template<class t>
class Vector: std::vector<t>
{
public:
using std::vector<t>::vector;
};
int main()
{
Vector<int> v;
return 0;
}
and it works fine on gcc 4.8. when passing the flag -std=c++11.
It sounds like your compiler cannot figure out what are you trying to accomplish with that using directive. Since inheriting constructors is a C++11 feature, I suggest you make sure that you are running your compiler in C++11 mode.