Тип C ++ не может быть решен ошибка

0

Я пытаюсь создать экземпляр класса в основной функции следующим образом:

#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 - Type' BookmarkRepository 'не может быть разрешен.

Я ненавижу C++, но на этой неделе у меня есть экзамен, поэтому небольшой совет о том, что не делать, чтобы избежать подобных ошибок, было бы чрезвычайно оценено. Спасибо !

  • 1
    Можете ли вы опубликовать MVCE ( stackoverflow.com/help/mcve )? Здесь слишком мало информации для всего этого кода
  • 0
    Извините, что вы подразумеваете под MVCE?
Показать ещё 1 комментарий
Теги:

1 ответ

6
Лучший ответ

Вам нужно указать тип шаблона:

BookmarkRepository<Bookmark>* repo = new BookmarkRepository<Bookmark>("./resources/bookmarks");
  • 0
    Ты был прав! Спасибо !
  • 0
    Если бы вы могли ответить мне, нормально ли иметь только заголовочный файл, как у меня для BookmarkRepository, а не файл cpp тоже?
Показать ещё 2 комментария

Ещё вопросы

Сообщество Overcoder
Наверх
Меню