开发者

template error message c++

开发者 https://www.devze.com 2023-04-09 20:26 出处:网络
I have the following header file: #ifndef DATABASE_H #define DATABASE_H #include <vector> #include <iostream>

I have the following header file:

#ifndef DATABASE_H
#define DATABASE_H


#include <vector>
#include <iostream>
#include <string>

#include "record.h"

using namespace std;

template <class value>
class Database {

  public:

    void write (ostream& out, DBScope scope) const;

    Database <value>() {};
    ~Database();

  private:

    vector <Record<value> > records;

};

#include "database.tem"

#endif

database.tem:

template <class value>
void Database<value>::write (ostream& out, DBScope scope) const {

 class vector <Record <value> >::iterator itr = records.begin();

 switch (scope) {

    case AllRecords:
      for (; itr != records.end(); itr++) out << itr;
      break;
    case SelectedRecords:
      for (; itr != records.end(); itr++) {
        if (itr.isSelected) out << itr;
      }
      break;

  }
}

I was running this header a long with another file and produced this error message, noting that in my header file I have another file called database.tem which is #include "database.tem" into my database.h file:

    database.tem: In member function 'void Database<value>::write (std:: ostream&, DBScope) const [with value = int]':
database.tem: In member function 'void Database<value>::write(std::ostream&, DBScope) const [with value = int]':
interactive.cpp:285:   instantiated from 'bool WriteCommand(Database<value>&) [with value = int]'
interactive.cpp:127:   instantiated from 'bool DispatchCommand(CommandT, Database<value>&) [with value = int]'
interactive.cpp:74:   instantiated from 'void MainLoop(Database<value>&) [with value = int]'
interactive.cpp:99:   instantiated from here
database.tem:6: error: conversion from '__gnu_cxx::__normal_iterator<const Record<int>*, std::vector<Record<int>, std::allocator<Record<int> > > >' to non-scalar type '__gnu_cxx::__normal_iterator<Record<int>*, std::vector<Record<int>, std::allocator<Record<int> > > >' requested
interactive.cpp:285:   instantiated from 'bool WriteCommand(Database<value>&) [with value = int]'
interactive.cpp:127:   instantiated from 'bool DispatchCommand(CommandT, Database<value>&) [with value = int]'
interactive.cpp:74:   instantiated from 'void MainLoop(Database<value>&) [with value = int]'
interactive.cpp:99:   instantiated from here
database.tem:11: error: no match for 'operator<<' in 'out << itr'

I have no idea what this messsage is talking about, can someone help开发者_C百科 me


The first error has to do that the method is const, hence records is const, and reconds.begin() returns a const_iterator.

template <class value>
void Database<value>::write (ostream& out, DBScope scope) const {

 vector <Record <value> >::const_iterator itr = records.begin();

The second error means that you probably meant to dereference the iterator:

for (; itr != records.end(); itr++) out << *itr;


It looks like you have only posted a partial error message but the error is probably that you need to write itr->isSelected rather than itr.isSelected.

Also it's preferable to use ++itr instead of itr++ when using iterators.


What you are trying to print is iterator object, not the value it refer to:

out << itr;

Replace that with

out << *itr;

Also make sure that class Record has either method or function defined:

ostream& out operator <<(ostream& out); // method

ostream& out operator <<(ostream& out, const Record &); //function
0

精彩评论

暂无评论...
验证码 换一张
取 消