K.K*_*K.K 0 c++ sqlite containers stl
我有以下程序来访问sqlite数据库并将表的内容放入LIST CONTAINER.
我想要的只是打印列表容器中的数据.但是我得到了这个错误.
error: expected primary-expression before ‘<<’ token
Run Code Online (Sandbox Code Playgroud)
以下文件是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
Run Code Online (Sandbox Code Playgroud)
}
下面是我的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
Run Code Online (Sandbox Code Playgroud)
以下是我调用函数的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;
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
error: expected primary-expression before ‘<<’ token
cout << it* << " ";
Run Code Online (Sandbox Code Playgroud)
您的代码中有两个"错误".第一个是其他人都指出的那个.这个
cout << it * << " ";
Run Code Online (Sandbox Code Playgroud)
应该是这个
cout << *it << " ";
Run Code Online (Sandbox Code Playgroud)
如果课程产生第二个错误
no match for ‘operator<<’ (operand types are ‘std::ostream
{aka std::basic_ostream<char>}’ and ‘SiteCode’)
std::cout << *it << " ";
Run Code Online (Sandbox Code Playgroud)
这实际上是在告诉你究竟是什么问题.您正在尝试将SiteCode对象输出到流上,但没有<<为SiteCode对象定义运算符.
您需要为SiteCode结构添加以下内容.
ostream& operator<< (ostream &out, SiteCode &site)
{
out << "(" << site.siteID << "," << site.siteCode << ")";
return out;
}
Run Code Online (Sandbox Code Playgroud)
在定义结构之后,在头文件中声明这个,因此:
struct SiteCode
{
int siteID;
int siteCode;
};
inline ostream& operator<< (ostream &out, SiteCode &site)
{
out << "(" << site.siteID << "," << site.siteCode << ")";
return out;
}
Run Code Online (Sandbox Code Playgroud)
现在,你将能够使用<<与任何SiteCode对任何流对象.
如何实际格式化对象的输出取决于您.我只是选择将其显示为元组.
| 归档时间: |
|
| 查看次数: |
82 次 |
| 最近记录: |