I included the header file of the template class in the header of the class that use it, adding it to the cpp file doesn't help (i get the same error messages). Details: I wrote a template class that represent a list of objects. this it's definition: template< class T > class obj_list { public: obj_list(); virtual ~obj_list(); void addObject(T *object); T getObjectAt(int i); void saveObjects(); int getNumOfObjects(); void close_file(); void setFilename(char *fileName); private: char* file_name; CObList objects; POSITION pos; CFile objects_file; int numOfObjects; };
It's implication came in the same header file (i heard that when i writing a template class I need to put the definition and implication in the header file). I have a class named info_dlg, and in it's header file I create a variable of the type of obj_list, by writing this line: obj_list user_s(); (user_info is a class that hold information about a user). In the cpp of info_dlg, I have some methods that in them I try to use some of obj_list's methods, but i get error messages like that: C:\My Documents\tel_op3\tel_op3\info_dlg.cpp(14) : error C2228: left of '.setFilename' must have class/struct/union type C:\My Documents\tel_op3\tel_op3\info_dlg.cpp(21) : error C2228: left of '.saveObjects' must have class/struct/union type C:\My Documents\tel_op3\tel_op3\info_dlg.cpp(41) : error C2228: left of '.addObject' must have class/struct/union type I hope this is enough details, and you could help me. Chen
chen_k
Posts
-
metodes in template class arn't recognize -
metodes in template class arn't recognizeI have in my project a template class. When I try to use a method in that class, then i get error messages like that: C:\My Documents\tel_op3\tel_op3\info_dlg.cpp(14) : error C2228: left of '.setFilename' must have class/struct/union type What could be the problem? Thanks, Chen
-
Question/problem regarding to templates in C++I wrote a template class that represents objects list. This is its header:
template< class T, char* filename >
class obj_list
{
public:
obj_list();
virtual ~obj_list();
void addObject(T *object);
T getObjectAt(int i);
void saveObjects();
int getNumOfObjects();
void close_file();
private:
CObList objects;
POSITION pos;
CFile objects_file;
int numOfObjects;};
When I want to create an object of the type of this class, by writing this line:
obj_list users
I get this error message: c:\my documents\tel_op3\tel_op3\info_dlg.h(19) : error C2964: invalid expression as template parameter that point on that line and that error message: c:\my documents\tel_op3\tel_op3\obj_list.h(13) : error C2973: 'obj_list' : invalid template argument 'filename' that point on the first line in the header (the one that conation '{') The template class passes the compile without any error message. What is wrong and how can i fix it? Thanks in advance, Chen. -- modified at 6:18 Wednesday 31st August, 2005
-
saving problem using serializion in C++I'm writing a project and in that project I have a class named user_info that represent information about users. I have another class named user_list, that represent a list of users, and use CObLis. The information that I place in the list I want to save on disk, so i use for that serialization. The problem is that when I want to save the information, than the program crash. This is the header of user_info: #include // replace with #define _CONSOLE when compiling for Windows NT #define _DOS #include class user_info : public CObject { DECLARE_SERIAL(user_info) public: user_info(); user_info(CString suser_name,CString spassword,int suser_id,CString sname,CString sfamily_name,int suser_type,int scity_id,int sarea_id,int sonline=0); virtual ~user_info(); // user_info() {} user_info( const user_info &s ) // copy ctor { user_name = s.user_name; password = s.password; user_id = s.user_id; family_name = s.family_name; user_type = s.user_type; city_id = s.city_id; area_id = s.area_id; online = s.online; } user_info& operator=( const user_info &s ) // assignment operator { user_name = s.user_name; password = s.password; user_id = s.user_id; family_name = s.family_name; user_type = s.user_type; city_id = s.city_id; area_id = s.area_id; online = s.online; return *this; } CString get_user_name(); CString get_password(); int get_user_id(); CString get_name(); CString get_family_name(); int get_user_type(); int get_city_id(); int get_area_id(); int get_statuse(); void test_list(); void Serialize (CArchive& ar); private: CString user_name; CString password; int user_id; CString name; CString family_name; int user_type; int city_id; int area_id; int online; }; This is the implication of the Serialize method in the user_indo.cpp file: IMPLEMENT_SERIAL(user_info, CObject, 0) void user_info::Serialize(CArchive& ar) { CObject::Serialize (ar); if (ar.IsStoring()) ar << user_name << password << user_id << name << family_name << user_type << city_id << area_id << online; else ar >> user_name >> password >> user_id >> name >> family_name >> user_type >> city_id >> area_id >> online; } This is the implication of the constructor of user_list: user_list::user_list() { char* FileName="user_list.dat"; user_list::numOfUsers =0; if (users_file.Open (FileName, CFile::modeRead)) { CAr
-
variables initialization in the constructorlet's say i have a class like this: class a { private: char name[10]; . . . } how do i give 'name' a value in the constructor? chen.