I have to pass a const (vector > from my main class to an object (Chatroom). I haven't worked a lot with unique_ptr's before. I only just started with the program so some functions still do nothing/are null but it worked until I started using the unique_ptr (Did it with a vector of strings first)
Main class (ChatApp.cpp):
#include <iomanip>
#include <stdio.h>
#include <iostream>
#include <memory>
#include "Chatroom.h"
#include "weerbot.h"
#include "weermens.h"
using namespace std;
//Atributes
Chatroom *chatroom;
vector<unique_ptr<Weerchatter> > deelnemers;
//Methods
void welcome();
void menu();
void launch();
int main(){
welcome();
menu();
}
void welcome(){
cout << "Test" << endl;
}
void menu(){
cout << "1.\tStoppen" << endl;
cout << "2.\tBot toevoegen" << endl;
cout << "3.\tMens toevoegen" << endl;
cout << "4.\tStart" << endl;
cout << "Keuze: ";
int keuze;
cin >> keuze;
unique_ptr<Weerchatter> bo(new Weerbot(" "));
unique_ptr<Weerchatter> me(new Weermens(" "));
switch(keuze){
case 1: exit(0);
case 2: deelnemers.push_back(move(bo)); welcome();cout << "\nBot toegvoegd\n\n";menu();
case 3: deelnemers.push_back(move(me)); welcome(); cout << "\nMens toegvoegd\n\n";menu();
case 4: launch(); break;
default: cout << "\nOngeldige keuze\n\n";menu();
}
}
/*
*Start de chatroom
*/
void launch(){
//Create chatroom
chatroom = new Chatroom(deelnemers, "test", 5); //The problems lies here
chatroom->start();
}
Chatroom.h
#ifndef Chatroom_h
#define Chatroom_h
#include <memory>
#include <vector>
#include <iomanip>
#include <stdio.h>
#include <iostream>
#include "weerchatter.h"
using namespace std;
class Chatroom
{
private:
vector<unique_ptr<Weerchatter> > deelnemers;
char* lognaam;
int aantalReplieken;
public:
Chatroom (const vector<unique_ptr<Weerchatter> > deelnemers1,char* lognaam1, int aantalReplieken1);
void start();
void listenToNext();
Chatroom(const Chatroom&) = delete;
Chatroom& operator=(const Chatroom&) = delete;
~Chatroom() = default;
};
#endif
Chatroom.cpp:
#include "chatroom.h"
using namespace std;
Chatroom::Chatroom(const (vector<unique_ptr<Weerchatter> > deelnemers1, char * lognaam1, int aantalReplieken1)){
lognaam = lognaam1;
for(int i = 0; i <deelnemers1.size();i++){
deelnemers.push_back(move(deelnemers1[i]));
} //The problems lies here
aantalReplieken = aantalReplieken1;
}
void Chatroom::start(){
system("cls");
cout << lognaam << " " << aantalReplieken << endl;
for(int i = 0; i <deelnemers.size();i++){
cout << i << "." << (*deelnemers[i]).geefNickname()<< endl;
}
listenToNext();
}
void Chatroom::listenToNext(){
cin.get();
listenToNext();
}
I explicitely have to use this method; Chatroom::Chatroom(const (vector > deelnemers1, char * lognaam1, int aantalReplieken1))
But I find it hard to implement it the right way. The unique_ptr means I can't copy it so i'll have to move? But the const lets me think I can't move the unique_ptr? The code aboves returns the following error. (I left out some classes whom I think have nothing to do with the problem) Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-ingw32\4.9.2\include\c++\bits\stl_construct.h
[Error] use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = Weerchatter; _Dp = std::default_delete]'
(and opens up the following code in Dev-c++)
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
/**
* Constructs an object in existing memory by invoking an allocated
* object's constructor with an initializer.
*/
#if __cplusplus >= 201103L
template<typename _T1, typename... _Args>
inline void
_Construct(_T1* __p, _Args&&... __args)
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
#else
template<typename _T1, typename _T2>
inline void
_Construct(_T1* __p, const _T2& __value)
{
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 402. wrong new expression in [some_]allocator::construct
::new(static_cast<void*>(__p)) _T1(__value);
}
Any idea what I do wrong?
Aucun commentaire:
Enregistrer un commentaire