如何定义sqlite3 struct的静态指针?C++

Dai*_*vys 1 c++ sqlite static pointers unresolved-external

我想要有sqlite3结构的静态指针,所以我可以打开一次连接到DB,在运行时执行一些查询并在程序出口关闭数据库连接.

(我链接了sqlite3 static lib,dll)

所以在我的班级标题中:

foo.h中:

#include "sqlite/sqlite3.h"

class foo
{
    public:
       static sqlite3 *db;
       static void connect();
}
Run Code Online (Sandbox Code Playgroud)

Foo.cpp中:

#include "foo.h"

sqlite3 foo::*db = nullptr;

foo::connect(){

   //sqlite3 *db;   //<-this works
   char *zErrMsg = 0;
   int rc;

   rc = sqlite3_open("test.db", &db);

   if( rc ){
      fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
      exit(0);
   }else{
      fprintf(stderr, "Opened database successfully\n");
   }
   //sqlite3_close(db); // close connection when program is exiting. Not here.

}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:LNK2001:未解析的外部符号"public static struct sqlite3*foo :: db"....

jua*_*nza 6

你有一个指向a的指针sqlite3,所以正确的定义语法就是

sqlite3* foo::db = nullptr;
Run Code Online (Sandbox Code Playgroud)

要不就

sqlite3* foo::db;
Run Code Online (Sandbox Code Playgroud)

请注意,sqlite3在取消引用之前,必须使其指向有效对象.