I have following program to access sqlite database and to get the content of a table into a LIST CONTAINER.
All I want is to print the data which is in the list container.But I get this ERROR.
error: expected primary-expression before ‘<<’ token
The below file is 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
}
Below is my DBAccess.h file:
#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
And below is my main.cpp from where I am calling the functions:
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;
}
The Error I get is:
error: expected primary-expression before ‘<<’ token
cout << it* << " ";
You have two "errors" in your code. The first is the one that everyone else has pointed out. This
cout << it * << " ";
should be this
cout << *it << " ";
Which if course generates the second error
no match for ‘operator<<’ (operand types are ‘std::ostream
{aka std::basic_ostream<char>}’ and ‘SiteCode’)
std::cout << *it << " ";
Which is actually telling you exactly what the problem is. You are trying to output a SiteCode
object onto the stream, but there is no <<
operator defined for a SiteCode
object.
You need to add the following for your SiteCode
struct.
ostream& operator<< (ostream &out, SiteCode &site)
{
out << "(" << site.siteID << "," << site.siteCode << ")";
return out;
}
Declare this in the header file, after the struct is defined, thus:
struct SiteCode
{
int siteID;
int siteCode;
};
inline ostream& operator<< (ostream &out, SiteCode &site)
{
out << "(" << site.siteID << "," << site.siteCode << ")";
return out;
}
And now you will be able to use <<
with any SiteCode
object on any stream.
How you actually format the output of the object is up to you. I just chose to display it as a tuple.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다