How do I use "<<" with my own struct?

K.K

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* << " ";
kdopen

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

AS3: How do i draw shapes in my own class

분류에서Dev

How do I replace certain PHP values with my own text?

분류에서Dev

How do I access general eclipse preferences in my own plugin

분류에서Dev

How do I access an external drive mounted on a machine on my own network?

분류에서Dev

How do i get my c++ program to "press keys" on its own?

분류에서Dev

How do I use bootstrap for my rails application?

분류에서Dev

How do I pass an array of struct to shader

분류에서Dev

How can I create my own spelling file for vim?

분류에서Dev

How should I handle the shebang when writing my own shell?

분류에서Dev

How should I write my own custom Annotation in Java

분류에서Dev

How do I use my work Outlook 2016 email with Outlook 365? Or use something other than outlook for my email client?

분류에서Dev

How to add my own .deb file to my local mirror to use with apt?

분류에서Dev

How do I get Safari 9 to use my new pinned tab icon?

분류에서Dev

How do I use SVP (Smooth Video Project) with my Media Player Classic?

분류에서Dev

How do I use my 30 GB Solid State Drive (SSD) with Ubuntu 14.04?

분류에서Dev

How do I use MapStateListener?

분류에서Dev

How can I use my backup of Thunderbird

분류에서Dev

How do I add this jQuery into my Javascript?

분류에서Dev

How do I display millisecond in my stopwatch?

분류에서Dev

How do I convert my string?

분류에서Dev

how do i make my code shorter

분류에서Dev

why do I have to declare an irrelevant struct file_handle variable before I can use that type?

분류에서Dev

How can I use my godaddy domain for my Django App

분류에서Dev

How can I make my own "shell commands" (e.g. mkdir/cd combo)?

분류에서Dev

How can I capture the "load" event by adding my own listener through ExecuteScript?

분류에서Dev

How can I connect the ASP.NET Identity database with my own database?

분류에서Dev

How can I connect on my own PC to a local FileZilla FTP server for testings?

분류에서Dev

How do I get my Links under my Links label?

분류에서Dev

How do I get my <li> centered in my nav

Related 관련 기사

  1. 1

    AS3: How do i draw shapes in my own class

  2. 2

    How do I replace certain PHP values with my own text?

  3. 3

    How do I access general eclipse preferences in my own plugin

  4. 4

    How do I access an external drive mounted on a machine on my own network?

  5. 5

    How do i get my c++ program to "press keys" on its own?

  6. 6

    How do I use bootstrap for my rails application?

  7. 7

    How do I pass an array of struct to shader

  8. 8

    How can I create my own spelling file for vim?

  9. 9

    How should I handle the shebang when writing my own shell?

  10. 10

    How should I write my own custom Annotation in Java

  11. 11

    How do I use my work Outlook 2016 email with Outlook 365? Or use something other than outlook for my email client?

  12. 12

    How to add my own .deb file to my local mirror to use with apt?

  13. 13

    How do I get Safari 9 to use my new pinned tab icon?

  14. 14

    How do I use SVP (Smooth Video Project) with my Media Player Classic?

  15. 15

    How do I use my 30 GB Solid State Drive (SSD) with Ubuntu 14.04?

  16. 16

    How do I use MapStateListener?

  17. 17

    How can I use my backup of Thunderbird

  18. 18

    How do I add this jQuery into my Javascript?

  19. 19

    How do I display millisecond in my stopwatch?

  20. 20

    How do I convert my string?

  21. 21

    how do i make my code shorter

  22. 22

    why do I have to declare an irrelevant struct file_handle variable before I can use that type?

  23. 23

    How can I use my godaddy domain for my Django App

  24. 24

    How can I make my own "shell commands" (e.g. mkdir/cd combo)?

  25. 25

    How can I capture the "load" event by adding my own listener through ExecuteScript?

  26. 26

    How can I connect the ASP.NET Identity database with my own database?

  27. 27

    How can I connect on my own PC to a local FileZilla FTP server for testings?

  28. 28

    How do I get my Links under my Links label?

  29. 29

    How do I get my <li> centered in my nav

뜨겁다태그

보관