我尝试在这样的主函数中实例化一个类:
#include <iostream>
#include<vector>
#include "./repository/BookmarkRepository.h"
#include "./domain/bookmark.h"
#include <list>
#include <iterator>
using namespace std;
using namespace domain;
using namespace repository;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
//here comes the error
BookmarkRepository<Bookmark>* repo = new BookmarkRepository("./resources/bookmarks");
return 0;
}
这是BookmarkRepository:
#ifndef BOOKMARKREPOSITORY_H_
#define BOOKMARKREPOSITORY_H_
#include <string>
#include <iostream>
#include <iterator>
#include <list>
#include <algorithm>
#include <fstream>
#include "../domain/bookmark.h"
using namespace domain;
using namespace std;
namespace repository{
template<class Entity>
class BookmarkRepository {
std::string filename;
std::list<Entity>* entities;
public:
BookmarkRepository(std::string filename) :
filename(filename) {
loadEntities();
}
void save(domain::Bookmark b){
this->entities.push_back(b);
ofstream fout(filename.c_str(), ios::app);
fout <<b<<"\n";
fout.close();
}
void update(int id,domain::Bookmark b);
void refresh();
void remove(int id);
private:
void loadEntities(){
string line;
ifstream fin(filename.c_str());
if (!fin.is_open()) {
throw "could not open file";
}
while (fin.good()) {
Bookmark b;
fin >> b;
}
fin.close();
}
};
}
#endif /* BOOKMARKREPOSITORY_H_ */
这是bookmark.h
#ifndef BOOKMARK_H_
#define BOOKMARK_H_
#include <string>
namespace domain {
class Bookmark {
int id;
std::string title;
std::string address;
public:
Bookmark(int id=0, const std::string& title="", const std::string& address="") :
id(id),title(title),address(address){
}
const int getId() const;
const std::string& getTitle() const;
void setId(int id);
const std::string& getAddress() const;
void setTitle(const std::string& type);
void setAddress(const std::string& address);
friend std::ostream& operator<<(std::ostream&, const Bookmark&);
friend std::istream& operator>>(std::istream&, Bookmark&);
};
}
#endif /* BOOKMARK_H_ */
和bookmark.cpp
#include "bookmark.h"
#include <sstream>
namespace domain {
const int Bookmark::getId() const {
return id;
}
void Bookmark::setId(int id) {
this->id = id;
}
void Bookmark::setTitle(const std::string& title) {
this->title = title;
}
const std::string& Bookmark::getTitle() const {
return title;
}
void Bookmark::setAddress(const std::string& address) {
this->address = address;
}
const std::string& Bookmark::getAddress() const {
return address;
}
std::ostream& operator <<(std::ostream& os, const Bookmark& b) {
os << b.getId() << "," << b.getTitle() << ","
<< b.getAddress();
return os;
}
std::istream& operator >>(std::istream& is, Bookmark& b) {
using namespace std;
string line, token, title,address;
int id;
is >> line;
istringstream ss(line);
getline(ss, token, ',');
stringstream(token)>>id;
getline(ss, token, ',');
title = token;
getline(ss, token, ',');
address = token;
b.setId(id);
b.setTitle(title);
b.setAddress(address);
return is;
}
}
我收到的错误实际上更复杂:
此行有多个标记 - 在'BookmarkRepository'之前预期','或';' - 未使用的变量'repo'[-Wunused-variable] - 无法将'int *'转换为'repository :: BookmarkRepository *' 初始化 - 'BookmarkRepository'之前的预期类型说明符 - 无法解析类型'BookmarkRepository'。
我讨厌C ++,但是本周我参加了一个考试,所以对于为了避免这种错误而不该做什么的一点建议将非常感激。 谢谢!
答案 0 :(得分:6)
您需要指定模板类型:
BookmarkRepository<Bookmark>* repo = new BookmarkRepository<Bookmark>("./resources/bookmarks");