0

I try to instantiate a class in a main function like this :

#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; } 

Here is the 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_ */ 

An here is the 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_ */ 

And the 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; } } 

The error that I receive is actually more complicated :

Multiple markers at this line - expected ‘,’ or ‘;’ before ‘BookmarkRepository’ - unused variable ‘repo’ [-Wunused-variable] - cannot convert ‘int*’ to ‘repository::BookmarkRepository*’ in initialization - expected type-specifier before ‘BookmarkRepository’ - Type 'BookmarkRepository' cannot be resolved.

I hate C++,but I have an exam this week so a little advice about what not to do in order to avoid this kind of errors would be grately appreciated . Thank you !

3
  • 1
    Can you post a MVCE (stackoverflow.com/help/mcve)? There's way too little information here for all that code Commented Jun 9, 2014 at 16:00
  • Sorry,what do you mean by MVCE? Commented Jun 9, 2014 at 16:01
  • There's a link right in my post. Commented Jun 9, 2014 at 16:02

1 Answer 1

6

You need to specify template type:

BookmarkRepository<Bookmark>* repo = new BookmarkRepository<Bookmark>("./resources/bookmarks"); 
Sign up to request clarification or add additional context in comments.

3 Comments

If you could answer me,is it ok to have only a header file like I have for BookmarkRepository and not a cpp file too?
It's not only OK, it's the only way. (Putting using namespace in a header file is a bad idea).
Thaks again ! I deleted using namespace !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.