LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1359|回复: 12

构造函数用explict并初始化std::runtime_err, 有问题。

[复制链接]
发表于 2006-8-14 20:59:58 | 显示全部楼层 |阅读模式
有三个文件sqlite_db.h/.cpp/, main.cpp

  1. #ifndef SQLITE_DB_H
  2. #define SQLITE_DB_H

  3. #include <string>
  4. #include <map>
  5. #include <vector>
  6. #include "sqlite.h"

  7. using namespace std;

  8. class sqlite_exception : public std::runtime_error
  9. {
  10.   public:
  11.     explicit sqlite_exception( const std::string &what )
  12.                :std::runtime_error( what )
  13.    { }
  14. };


  15. typedef std::map<std::string,std::string> sqlite_fields;
  16. typedef std::vector<sqlite_fields> sqlite_result;

  17. class sqlite_db
  18. {
  19.   private:
  20.     sqlite* db_;
  21.     static int sqlite_callback(void *param,int colcount,char **cols,char **colnames);
  22.   public:
  23.     sqlite_db():db_(NULL){}
  24.     virtual ~sqlite_db(){if(db_)close();}
  25.     const char *version()const{return SQLITE_VERSION;}
  26.     void open(const std::string& db);
  27.     void close();
  28.     sqlite_result exec_sql(const std::string& query);
  29.     void exec_dml(const std::string& query);
  30. };

  31. #endif
复制代码

编译如下:

  1. [root@root DB]# g++ -I/usr/local/include -c sqlite_db.cpp -o sqlite_db.o
  2. In file included from sqlite_db.cpp:1:
  3. sqlite_db.h:12: error: expected class-name before '{' token
  4. sqlite_db.h: In constructor `sqlite_exception::sqlite_exception(const std::string&)':
  5. sqlite_db.h:14: error: expected class-name before '(' token
  6. [root@root DB]#
复制代码
发表于 2006-8-14 21:20:53 | 显示全部楼层
public class
回复 支持 反对

使用道具 举报

发表于 2006-8-15 10:05:38 | 显示全部楼层
headfile "stdexcept" ??
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-8-15 10:06:26 | 显示全部楼层
where? I have tried for serval times for adding "public clas" to place in where should be added, but in vail eventually.
回复 支持 反对

使用道具 举报

发表于 2006-8-15 10:15:07 | 显示全部楼层
不需要 stdexcept 头文件吗?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-8-15 11:57:51 | 显示全部楼层
已经加上<stdexcept>。

  1. [root@root DB]# g++ -I/usr/local/include -c sqlite_db.cpp -o sqlite_db.o
  2. In file included from sqlite_db.cpp:1:
  3. sqlite_db.h:14: error: expected `,' or `...' before '&' token
  4. sqlite_db.h:14: error: ISO C++ forbids declaration of `string' with no type
  5. sqlite_db.h: In constructor `sqlite_exception::sqlite_exception(int)':
  6. sqlite_db.h:15: error: no matching function for call to `std::runtime_error::runtime_error(<unknown type>)'
  7. 下面是sqlite_db.cpp的错误,都是因为sqlite_execption中转化错误或函数不匹配,所以没有列出来。
复制代码
回复 支持 反对

使用道具 举报

发表于 2006-8-15 14:58:04 | 显示全部楼层
呵呵,我以为继承要写成class sqlite_exception : public class std::runtime_error
不过发现不用
回复 支持 反对

使用道具 举报

发表于 2006-8-15 16:13:04 | 显示全部楼层
Post by sybaselu
已经加上<stdexcept>。

  1. [root@root DB]# g++ -I/usr/local/include -c sqlite_db.cpp -o sqlite_db.o
  2. In file included from sqlite_db.cpp:1:
  3. sqlite_db.h:14: error: expected `,' or `...' before '&' token
  4. sqlite_db.h:14: error: ISO C++ forbids declaration of `string' with no type
  5. sqlite_db.h: In constructor `sqlite_exception::sqlite_exception(int)':
  6. sqlite_db.h:15: error: no matching function for call to `std::runtime_error::runtime_error(<unknown type>)'
  7. 下面是sqlite_db.cpp的错误,都是因为sqlite_execption中转化错误或函数不匹配,所以没有列出来。
复制代码


感觉怪怪的, 全部帖上来看看吧....
回复 支持 反对

使用道具 举报

发表于 2006-8-15 16:14:07 | 显示全部楼层
Post by x11
呵呵,我以为继承要写成class sqlite_exception : public class std::runtime_error
不过发现不用

兄弟在上海?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-8-15 16:40:20 | 显示全部楼层
Ok , here we go, go throughly the whole source code :
sqlite_db.h//

  1. #ifndef SQLITE_DB_H
  2. #define SQLITE_DB_H

  3. #include <string>
  4. #include <map>
  5. #include <vector>
  6. #include "sqlite.h"
  7. #include <stdexcept>


  8. class sqlite_exception : public std::runtime_error
  9. {
  10. public:
  11.     explicit sqlite_exception(const string& what) : std::runtime_error( what )
  12.     {}
  13. };

  14. typedef std::map<std::string,std::string> sqlite_fields;
  15. typedef std::vector<sqlite_fields> sqlite_result;

  16. class sqlite_db
  17. {
  18.   private:
  19.     sqlite* db_;
  20.     static int sqlite_callback(void *param,int colcount,char **cols,char **colnames);
  21.   public:
  22.     sqlite_db():db_(NULL){}
  23.     virtual ~sqlite_db(){if(db_)close();}
  24.     const char *version()const{return SQLITE_VERSION;}
  25.     void open(const std::string& db);
  26.     void close();
  27.     sqlite_result exec_sql(const std::string& query);
  28.     void exec_dml(const std::string& query);
  29. };

  30. #endif

复制代码

sqlite_db.cpp//

  1. #include "sqlite_db.h"

  2. void sqlite_db::open(const std::string& db)
  3. {
  4.   if(db_)close();
  5.   char *errmsg = NULL;
  6.   db_ = sqlite_open(db.c_str(),0,&errmsg);
  7.   if(db_ == 0)
  8.   {
  9.     std::string error(errmsg);
  10.     sqlite_freemem(errmsg);
  11.     throw sqlite_exception(error);
  12.   }
  13.   if(errmsg != NULL) sqlite_freemem(errmsg);
  14. }

  15. void sqlite_db::close()
  16. {
  17.   if(db_)sqlite_close(db_);
  18.   db_ = NULL;
  19. }

  20. sqlite_result sqlite_db::exec_sql(const std::string& query)
  21. {
  22.   if(!db_)throw sqlite_exception("No opened database");
  23.   char* errmsg = NULL;
  24.   sqlite_result result;
  25.   int res = sqlite_exec(db_,query.c_str(),sqlite_callback,(void*)&result,&errmsg);
  26.   if(res != SQLITE_OK)
  27.   {
  28.     std::string error(errmsg);
  29.     sqlite_freemem(errmsg);
  30.     throw sqlite_exception(error);
  31.   }
  32.   if(errmsg != NULL) sqlite_freemem(errmsg);
  33.   return result;
  34. }

  35. void sqlite_db::exec_dml(const std::string& query)
  36. {
  37.   if(!db_)throw sqlite_exception("No opened database");
  38.   char* errmsg = NULL;
  39.   int res = sqlite_exec(db_,query.c_str(),sqlite_callback,(void*)NULL,&errmsg);
  40.   if(res != SQLITE_OK)
  41.   {
  42.     std::string error(errmsg);
  43.     sqlite_freemem(errmsg);
  44.     throw sqlite_exception(error);
  45.   }
  46.   if(errmsg != NULL) sqlite_freemem(errmsg);
  47. }

  48. int sqlite_db::sqlite_callback(void *param,int colcount,char **cols,char **colnames)
  49. {
  50.   sqlite_result* result = reinterpret_cast<sqlite_result*>(param);
  51.   sqlite_fields f;
  52.   for(int i = 0;i < colcount;++i)
  53.   {
  54.     f[colnames[i]] = cols[i];
  55.   }
  56.   result->push_back(f);
  57.   return 0;
  58. }

复制代码

sqlite_db_main.cpp//

  1. #include <iostream>
  2. #include <string>
  3. #include <conio.h>

  4. #include "sqlite_db.h"

  5. int main()
  6. {
  7.   sqlite_db db;
  8.   try
  9.   {
  10.     db.open("test.db");
  11.     std::cout<<"sqlite version = "<<db.version()<<std::endl;

  12.     // First we create the table
  13.     std::string query("CREATE TABLE customers (Id integer,Name varchar(16)," \
  14.          "City varchar(16));");
  15.     db.exec_dml(query);

  16.     // Add some records to the table
  17.     query = "INSERT INTO customers VALUES (2004001,'Smith','Brussels');";
  18.     db.exec_dml(query);
  19.     query = "INSERT INTO customers VALUES (2004002,'Powell','London');";
  20.     db.exec_dml(query);
  21.     query = "INSERT INTO customers VALUES (2004003,'Brown','New-York');";
  22.     db.exec_dml(query);

  23.     // Read the records back
  24.     query = "SELECT * FROM customers;";
  25.     sqlite_result res = db.exec_sql(query);
  26.     for(sqlite_result::size_type i = 0;i < res.size();++i)
  27.     {
  28.       sqlite_fields row = res[i];
  29.       for(sqlite_fields::const_iterator it = row.begin();it != row.end();++it)
  30.       {
  31.         std::cout<<(*it).first<<" = "<<(*it).second<<std::endl;
  32.       }
  33.       std::cout<<std::endl;
  34.     }
  35.   }
  36.   catch(sqlite_exception &ex)
  37.   {
  38.     std::cout<<ex.what()<<std::endl;
  39.   }

  40.   getch();
  41.   return 0;
  42. }
  43.                                                                            
复制代码

谢过先了,想用封装过的sqite3做点东东。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表