我有以下程序来访问sqlite数据库并将表的内容放入LIST CONTAINER。

我想要的只是打印列表容器中的数据。但是出现此错误。

error: expected primary-expression before ‘<<’ token


下面的文件是DBAccess1.cpp

#include <iostream>
#include <sqlite3.h>
#include <list>
#include <iterator>
#include <algorithm>
#include <string>
#include <cstring>
#include <sstream>

#include "DBAccess1.h"

bool sqliteDB::GET_ALL_Site_Code(list<SiteCode>& Site_Code_list)
{
        sqlite3 *db;
        const char *sql;
        sqlite3_stmt * stmt;

        int rc = sqlite3_open("/DBsqlite3/empdbv3.db", &db);

        sql = "SELECT * FROM SiteCode;";

        rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);

        while(sqlite3_step(stmt)==SQLITE_ROW) {

             int column = sqlite3_column_count(stmt);

             for(int i = 0; i < column; i++)
             {
                 int A  = sqlite3_column_int(stmt, 0);
                 int B = sqlite3_column_int(stmt, 1);

                 SiteCode info;
                 info.siteID = A;
                 info.siteCode = B;

                 cout<<"Preparing to push data into List"<<endl;
                 Site_Code_list.push_back(info);
                 cout<<"Data was pushed successfully"<<endl;
             }//FOR LOOP ENDS HERE

        }// WHILE LOOP ENDS HERE

    sqlite3_finalize(stmt);
    sqlite3_close(db);

    return true;
}

//=================================XX=============================//

void sqliteDB::printList()
{

     int s = Site_Code_list.size();
     cout << "The size of List is :" << s << endl;

     for( list<SiteCode> :: iterator it = Site_Code_list.begin(); it !=  Site_Code_list.end(); it++)

     cout << it* << " "; //The ERROR occurs here


}

以下是我的DBAccess.h文件:

#ifndef DBAccess1_HH
#define DBAccess1_HH

#include <iostream>
#include <sqlite3.h>
#include <list>
#include <iterator>
#include <algorithm>
#include <cstring>
#include <sstream>
#include <string>

using namespace std;

struct SiteCode
{
  int siteID;
  int siteCode;
};

class sqliteDB {

public:

list<SiteCode> Site_Code_list;

bool GET_ALL_Site_Code(list<SiteCode>& Site_Code_list);

void printList();

};

#endif


以下是我在其中调用函数的main.cpp:

int main()

{

    sqliteDB object1;

    list<SiteCode> Site_Code_list;

    object1.GET_ALL_Site_Code(Site_Code_list);

    object1.printList();

    cout << "\n\nAll the statement were executed properly\n\n";

   return 0;
}


我得到的错误是:

   error: expected primary-expression before ‘<<’ token
   cout << it* << " ";

最佳答案

您的代码中有两个“错误”。第一个是其他所有人都指出的那个。这个

cout << it * << " ";


应该是这个

cout << *it << " ";


如果课程会产生第二个错误

no match for ‘operator<<’ (operand types are ‘std::ostream
{aka std::basic_ostream<char>}’ and ‘SiteCode’)
std::cout << *it << " ";


实际上,这实际上是在告诉您问题所在。您试图将SiteCode对象输出到流上,但是没有为<<对象定义SiteCode运算符。

您需要为SiteCode结构添加以下内容。

ostream& operator<< (ostream &out, SiteCode &site)
{
    out << "(" << site.siteID << "," << site.siteCode << ")";
    return out;
}


在定义结构之后,在头文件中声明它,因此:

struct SiteCode
{
  int siteID;
  int siteCode;
};

inline ostream& operator<< (ostream &out, SiteCode &site)
{
    out << "(" << site.siteID << "," << site.siteCode << ")";
    return out;
}


现在,您将可以在任何流上将<<与任何SiteCode对象一起使用。

如何实际格式化对象的输出取决于您。我只是选择将其显示为元组。

10-08 17:26