J
Jeff Chang
Hi all,
Below is the sample c++ code that cause error C4346 in vs-2009 pro. I have a
"Record" struct within the Database class, and has a "Retrive(int n)" member
function that return "Record*" type. If I have my implementation code
defined within the class then everything works fine. However if I put it
outside the class I get error C4346. I prefer to put the implementation code
outside the class to keep the code clean. Please help.
Thanks
Jeff
//---------------------------------------------------------------------------
#include <iostream>
#include <string>
using namespace std;
const int Maximum = 100;
//---------------------------------------------------------------------------
// Code-1: Database class definition, has a nested "Record" struct inside
//---------------------------------------------------------------------------
template<typename Object>
class Database {
public:
struct Record {
Object ID;
Record( Object a=Object() ) : ID(a) {}
};
private:
Record items[Maximum];
public:
int Count;
Database() : Count(0) { }
// the following declaration with Code-2 definition will
cause error C4346
Record* Retrieve(const int n);
// however if I replace the code above and those Code-2 with
this the error is gone
// Record* Retrieve(const int n) {
// return &items[n];
// }
void Add(const Record& d) {
if ( Count<Maximum ) items[Count++] = d;
}
};
//---------------------------------------------------------------------------
// Code-2: Retrieve() definition with the return type of "Record".
// Receive the following error when trying to compile
// warning C4346: 'Database<Object>::Record' : dependent name is not a
type
// 1> prefix with 'typename' to indicate a type
// error C2143: syntax error : missing ';' before
'Database<Object>::Retrieve'
// error C4430: missing type specifier - int assumed. Note: C++ does
not support default-int
// fatal error C1903: unable to recover from previous error(s);
stopping compilation
//---------------------------------------------------------------------------
template<typename Object>
Database<Object>::Record*
Database<Object>::Retrieve(const int n) {
return &items[n];
}
//---------------------------------------------------------------------------
// Main Driver
//---------------------------------------------------------------------------
int main(int argc, char* argv[]) {
typedef Database<int>::Record rec;
Database<int> lists;
lists.Add( rec(12) );
lists.Add( rec(13) );
lists.Add( rec(15) );
lists.Add( rec(18) );
lists.Add( rec(17) );
cout << "Record ID\n";
for(int i = 0; i < lists.Count; i++)
cout << lists.Retrieve(i)->ID << endl;
cout << endl;
system("pause");
return 0;
}
//---------------------------------------------------------------------------
// Program Output
//---------------------------------------------------------------------------
Record ID
12
13
15
Press any key to continue . . .
Below is the sample c++ code that cause error C4346 in vs-2009 pro. I have a
"Record" struct within the Database class, and has a "Retrive(int n)" member
function that return "Record*" type. If I have my implementation code
defined within the class then everything works fine. However if I put it
outside the class I get error C4346. I prefer to put the implementation code
outside the class to keep the code clean. Please help.
Thanks
Jeff
//---------------------------------------------------------------------------
#include <iostream>
#include <string>
using namespace std;
const int Maximum = 100;
//---------------------------------------------------------------------------
// Code-1: Database class definition, has a nested "Record" struct inside
//---------------------------------------------------------------------------
template<typename Object>
class Database {
public:
struct Record {
Object ID;
Record( Object a=Object() ) : ID(a) {}
};
private:
Record items[Maximum];
public:
int Count;
Database() : Count(0) { }
// the following declaration with Code-2 definition will
cause error C4346
Record* Retrieve(const int n);
// however if I replace the code above and those Code-2 with
this the error is gone
// Record* Retrieve(const int n) {
// return &items[n];
// }
void Add(const Record& d) {
if ( Count<Maximum ) items[Count++] = d;
}
};
//---------------------------------------------------------------------------
// Code-2: Retrieve() definition with the return type of "Record".
// Receive the following error when trying to compile
// warning C4346: 'Database<Object>::Record' : dependent name is not a
type
// 1> prefix with 'typename' to indicate a type
// error C2143: syntax error : missing ';' before
'Database<Object>::Retrieve'
// error C4430: missing type specifier - int assumed. Note: C++ does
not support default-int
// fatal error C1903: unable to recover from previous error(s);
stopping compilation
//---------------------------------------------------------------------------
template<typename Object>
Database<Object>::Record*
Database<Object>::Retrieve(const int n) {
return &items[n];
}
//---------------------------------------------------------------------------
// Main Driver
//---------------------------------------------------------------------------
int main(int argc, char* argv[]) {
typedef Database<int>::Record rec;
Database<int> lists;
lists.Add( rec(12) );
lists.Add( rec(13) );
lists.Add( rec(15) );
lists.Add( rec(18) );
lists.Add( rec(17) );
cout << "Record ID\n";
for(int i = 0; i < lists.Count; i++)
cout << lists.Retrieve(i)->ID << endl;
cout << endl;
system("pause");
return 0;
}
//---------------------------------------------------------------------------
// Program Output
//---------------------------------------------------------------------------
Record ID
12
13
15
Press any key to continue . . .