Hello,
I am having a couple of issues at the moment. I declared a vector (vector<char> str) and want an iterator to access it. A book and this site (
http://www.cplusplus.com/reference/stl/vector/begin.html) say that this is the correct syntax:
1 2 3 4 5 6
|
#include <vector>
/* ... */
vector<char> str;
vector<char>::iterator loc;
|
And it gives this error:
error C2955: 'std::iterator' : use of class template requires template argument list
C:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE\xutility(688) : see declaration of 'std::iterator'
Second issue: base class pointers.
I have a bunch of derived classes, say the letters a through d derived from myClass (ex.
class a : public myClass;
).
Inside the base class, I want to declare a base class pointer (myClass *p), and allocate memory for a derived class.
Heres some code to illistrate my problem:
header1.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#ifndef HEADER1_H
#define HEADER1_H
// foward declaration
class a;
class b;
class c;
class d;
class myClass
{
int test;
myClass *p;
public:
bool allocFunc(int test);
};
bool myClass::allocFunc(int test)
{
switch(test)
{
case 1:
try{
p = new a;
}catch(...){
return false;
}
break;
case 2:
try{
p = new b;
}catch(...){
return false;
}
break;
case 3:
try{
p = new c;
}catch(...){
return false;
}
break;
case 4:
try{
p = new d;
}catch(...){
return false;
}
break;
}
}
#endif /* HEADER1_H */
|
header2.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#ifndef HEADER2_H
#define HEADER2_H
// foward declaration
class myClass;
class a : public myClass
{
/* ... */
};
class d : public myClass
{
/* ... */
};
class c : public myClass
{
/* ... */
};
class d : public myClass
{
/* ... */
};
#endif /* HEADER2_H */
|
And with these I'm getting an error similar to:
error C2440: '=' : cannot convert from 'a *' to 'myClass *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Any ideas on these two errors? I can post some actual code I am using, but I thought it would be simpler to write some unobfuscated stuff. Let me know what you think,
enduser