C++ iterator problems - c++

I have the following member data
vector<State<T>*> activeChildren;
I want to clean-up these pointers in my destructor
StateContainer<T>::~StateContainer() {
vector<State<T>*>::iterator it =
activeChildren.begin();
while(it!=activeChildren.end()) {
State<T>* ptr = *it;
it = activeChildren.erase(it);
delete ptr;
}
}
I get the following error from g++ 4.3.2 on Ubuntu:
./fsm2/StateContainer.cpp: In destructor ‘virtual ervan::StateContainer<T>::~StateContainer()’:
../fsm2/StateContainer.cpp:24: error: expected `;' before ‘it’
../fsm2/StateContainer.cpp:25: error: ‘it’ was not declared in this scope
Can anyone tell me what I've done wrong? I get this error in two more places where I use iterator loops, but not when I use for_each(...)

Looks like typename time again - I think you need:
typename vector<State<T>*>::iterator it = ...
A heuristic for g++ users - when you see this message in template code:
expected `;' before ‘it’
it is a pretty good bet that the thing in front of the 'it' is not being seen by the compiler as a type and so needs a 'typename' added.

It's a parsing issue. In this code, vector<State<T>*>::iterator is a nested dependent type.
Until you know what T is (and T isn't known at parse time), the parser/compiler doesn't realize that iterator is a type (could be a static member variable, for all it knows).
Therefore, you must prefix the definition with typename as a hint to tell the compiler that for all vector<State<T>*>, vector<State<T>*>::iterator is a type name.

Related

Overriding operators/begin/end of a custom Array class

For an exercise we have to implement an array with custom bounds in various different languages, and one of them happens to be c++. Unfortunately i have never had to deal with c++ and am now struggling to get it done. I believe i have the right "core" of the header finished. The idea is that the user specifies bounds in a template and the class fakes this with help of an inner array. We were specifically told to overwrite the [] operator and implement begin() and end() functions. I am currently stuck at the visual studio debugger saying the following:
Error C2228 left of '.end' must have class/struct/union
The gcc compiler is saying:
In file included from Aufgabe1.test.cpp:2:
array.h: In instantiation of 'typename std::__cxx11::list<T>::iterator Array<T, L, H>::begin() [with T = int; int L = -2; int H = 1; typename std::__cxx11::list<T>::iterator = std::_List_iterator<int>]':
Aufgabe1.test.cpp:16:15: required from here
array.h:29:25: error: request for member 'begin' in '((Array<int, -2, 1>*)this)->Array<int, -2, 1>::innerArray', which is of non-class type 'int [4]'
return innerArray.begin;
~~~~~~~~~~~^~~~~
array.h: In instantiation of 'typename std::__cxx11::list<T>::iterator Array<T, L, H>::end() [with T = int; int L = -2; int H = 1; typename std::__cxx11::list<T>::iterator = std::_List_iterator<int>]':
Aufgabe1.test.cpp:16:15: required from here
array.h:33:25: error: request for member 'end' in '((Array<int, -2, 1>*)this)->Array<int, -2, 1>::innerArray', which is of non-class type 'int [4]'
return innerArray.end;
~~~~~~~~~~~^~~
Simply put i have no way of continuing from here since searching for these errors bring no clarification for someone who worked primarily with java so far. How can i solve thse issues and is my implementation of this Array otherwise correct ?
EDIT:
Had to remove the code
The first error message you mention explains the problem fairly well:
Error C2228 left of '.end' must have class/struct/union
The second group of messages tells you where this occurs, specifically when you try to use innerArray.end. The thing to the left of ".end" is innerArray, which is not declared as a class, struct, or union. (It's an array of T.)
So this comes down to the fact that (C-style) arrays do not have member functions. If you need an "end" function, maybe you should look into std::array, which wraps C-style arrays in a class. (If the requirement is to overwrite certain functions instead of implement them, this is the "array" your instructor likely had in mind.)

error: 'noexcept' does not name a type

Seems like it never happened to anyone so I guess I'm doing something wrong...
The error I get is:
error: expected ';' at end of member declaration
error: 'noexcept' does not name a type
the code is this:
void* operator new (size_t size) noexcept
{return foo(size);}
also the code is in macro actually, if it matters.
Thanks!

Why does access to nested types affect member resolution in C++?

Assume I have a simple structure...
struct A {
std::vector<uint32_t> v;
};
And I want to find the size of the vector element type...
sizeof(A::v); // legal
sizeof(decltype(A().v)::value_type); // legal
sizeof(A::v::value_type) // illegal
Error message...
example.cpp:18:44: error: no member named 'v' in 'A'
Why does the third version fail? I don't understand how adding another layer of scope resolution causes the more broad scope resolution to fail. In fact, the second example seems equivalent to the failing example, but obviously the compiler disagrees.
This is a what-if/language question. I am not trying to solve a particular problem with vector sizes but rahter understand this oddity of the language.
Update
Here is another example from an answer below that seems to further my confusion...
sizeof(decltype(A::v)::value_type); // legal
How is decltype(A::v)::value_type different from A::v::value_type in this context?
I don't think you can get a typename from a variable using the :: token. Ignore sizeof and ignore A::v. The following doesn't work:
A a; // A is some type with a public typedef for value_type in its declaration
A::value_type b; // ok
a::value_type c; // error
GCC 4.7.1 gives me this error:
x.cpp:9:1: error: 'a' does not name a type
sizeof makes this confusing only because it is a compile time construct that accepts both types and variables as its parameter.
Correct me if I'm wrong but doesn't the last one fail because you are accessing an instance in the last one whereas in the second one you get the class name from the decltype(). If you wrap the third one in a decltype it works
sizeof(decltype(A::v)::value_type);

Having problem passing member function pointers to templatized member function on gcc

I'm having problem passing member function pointers to templatized member function on gcc. Does anyone know how to modify the code below to get gcc to accept what I am trying to do?
class Foo
{
public:
template <class C, class R>
void Execute(R(typename C::*memFn)())
{
}
};
I get the following errors when trying to compile the code:
test.cpp:40: error: 'memFn' was not declared in this scope
test.cpp:40: error: expected primary-expression before '(' token
test.cpp:40: error: expected identifier before '*' token
test.cpp:40: error: expected '(' before '*' token
test.cpp:40: error: 'memFn' was not declared in this scope
test.cpp:40: error: variable or field 'Execute' declared void
The version of gcc that I am using is 4.4.2.
Thank you very much for your help!
You don't need typename. Remove that and it should work. (I tested it on gcc 4.3.2).

Error while writing static function?

Error while writing static function.
static int function_name ( const Reference< data_type>* ptr)
{
}
when i wrote a static functin which takes Reference pointer then ir gives me following error
error: ISO C++ forbids declaration of 'Reference' with no type
error: expected ',' or '...' before '<' token
The compiler is complaining because it doesn't know what a Reference<T> is. Either you've forgotten to #include the header file that it is defined in, or you've forgotten to forward-declare it.
You presumably mean Reference to be a template but the compiler doesn't see it as one.