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.
Related
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.
My problem is illustrated by the example below:
#include <vector>
template <class T>
class TestNest{
public:
std::vector<T> m_list;
class InsideNest{
const TestNest<T>* m_test;
decltype(m_test->m_list.begin()) m_iter;
public:
InsideNest(const TestNest<T>* source)
:m_test(source)
,m_iter(source->m_list.begin())
{}
};
};
int main(int argc, char *argv[])
{
TestNest<int> outside;
TestNest<int>::InsideNest inside(&outside);
}
the part that does not compile (at least not in MSVC2013) is decltype(m_test->m_list.begin()). Any idea how I can get around the problem?
EDIT: changed code to show main() and #include
To close the issue. This is a shortcoming of MSVC2013. It will resolve decltype() before "working out" the full type of members so inside decltype any access to methods is a compiler error.
Even using global template functions (e.g. decltype(std::begin(m_list))) won't work.
Other, more modern, compilers work.
I have some trouble with vector in class.
#include <vector>
using namespace std;
class sth {
vector<int> tab();
public:
void add(int i);
};
void sth::add(int i){
tab.push_back(i);
}
int main() {
sth a;
a.add(10);
return 0;
}
That simple code causes 11 5 [Error] '((sth*)this)->sth::tab' does not have class type
Any ideas what is going on?
The following is actually a function declaration:
vector<int> tab();
If you want to declare an std::vector, it should be:
vector<int> tab;
That simple code causes
11 5 [Error] '((sth*)this)->sth::tab' does not have class type
Any ideas what is going on?
What you have used is a class member function declaration:
vector<int> tab();
As I can read from your add() method implementation, you wanted to declare (and initialize?) a class member variable. To achieve this, the declaration syntax looks like this:
vector<int> tab;
To initialize your class member variables (even using their default constructors), you have to use the constructor methods member initialization list for the definition
sth::sth() : tab() {}
For the current c++ standard, you can also use a direct initialization with one of the following forms
vector<int> tab{};
vector<int> tab{1,2,3,4,5};
vector<int> tab = vector<int>();
I’m trying to puzzle out how clang determines what C++ template static member variables require instantiation, and I’m seeing some behavior that has me confused.
Given the following code:
#include <stdio.h>
#include <typeinfo>
int report(const char *name)
{
printf("Reporting class: %s\n", name);
return 0;
}
template<typename ReportedClass>
class reported_class
{
public:
reported_class()
{
_reported_instances++;
}
private:
static int _reported_instances;
};
template<typename ReportedClass>
int reported_class<ReportedClass>::_reported_instances = report(typeid(ReportedClass).name());
class foo : reported_class<foo>
{
public:
int baz() { return 0; }
};
class bar : reported_class<bar>
{
public:
bar() { }
int baz() { return 0; }
};
int main(int argc, char **argv)
{
return 0;
}
When I run it, I see the following:
$ c++ -v
Apple LLVM version 5.0 (clang-500.0.68) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix
$ c++ test.cpp
$ ./a.out
Reporting class: 3bar
$
Why is the static for reported_class being instantiated, but not the one for foo? The only difference appears to be the presence of a constructor, but I’d expect the reported_class constructor to be called in either case (therefore forcing static instantiation due to use in the constructor). Is there a reason for this in the standard that I’m not aware of, and is this something that can be relied upon?
gcc-4.7.3 displays the same behavior, so I assume that this is something that I"m misunderstanding.
Apparently in the not shown main function you're not instantiating either class.
Then there's no reason for the compiler to generate a default constructor for class foo.
And without that, there's no code that instantiates reported_class<foo>.
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