代码如下:
//sqlite_interface.h
class SqlteOp {
private:
sqlite3 *db;
public:
SqlteOp(string dbname);
~SqliteOp();
void print();
}
//sqlite_interface.cpp
SqliteOp::SqliteOp(string dbname)
{
cout<<"db name is: "<<dbname<<endl;
sqlite3_open(dbname.c_str(), &db);
...
}
SqliteOp::print()
{
cout<<"Sqlite Operation print function"<<endl;
...
}
SqliteOp::~SqliteOp()
{
cout<<"destroy sqlite op object"<<endl;
sqlite3_close(db);
}
//main.cpp
int main(int argc, char **argv)
{
SqliteOp sqliteOp(string(argv[1]));
sqliteOp.print();
return 0;
}
//编译
dk@dkos:~ g++ --std=c++11 main.cpp sqlinteop.cpp -I /usr/local/include -L /usr/local/lib -lsqlite3 -lpthread -ldl
//报错
main.cp:14:13: error: request for member 'print' in 'sqliteOp', which is of non-class type.......
恶心,非常恶心,这种错误太古怪了,让人觉得这语言太难用了,话费的编译调试成本也太大了….,无语了,看解决方法,将g++贴换成clang++尝试编译(为什么用clang,因为clang能打印出更好的错误提示信息),看具体提示
dk@dkos:~ clang++ --std=c++11 main.cpp sqlinteop.cpp -I /usr/local/include -L /usr/local/lib -lsqlite3 -lpthread -ldl
//提示如下
main.cpp:14:23: note: add a pair of parentheses to declare a variable
SqliteOp sqliteOp(string(argv[1]));
^
( )
按照提示加上之后编译通过,我猜可能编译器把这个定义只当做了函数声明,而没有把它当做一个类的定义。
类似问题后来我google一下发现有人也撞上了,但是他(她)解释的更清楚一些,链接如下
http://dehun.bitbucket.org/articles/23_oct_2013-Error%20which%20is%20of%20non-class%20type.html