Reputation:
I have 2 Classes in ssservice.h
:
class ssverbindungsdaten {
public:
ssverbindungsdaten();
~ssverbindungsdaten();
int id;
string test;
};
class softswitch {
public:
softswitch();
~softswitch();
list<ssverbindungsdaten> holeAlleRohdaten();
};
My ssservice.ccp
:
list<ssverbindungsdaten> softswitch::holeAlleRohdaten() {
//ssverbindungsdaten * ssv = new ssverbindungsdaten;
list<ssverbindungsdaten> *listTmp = new list<ssverbindungsdaten>;
return *listTmp;
}
And my main.cpp:
int main() {
std::cout << "Starte ...";
softswitch *ss = new softswitch;
list<ssverbindungsdaten> *liSSVVerbinngsdaten = new list<ssverbindungsdaten>();
liSSVVerbinngsdaten = ss->holeAlleRohdaten();
}
But there is an error in main.cpp
:
cannot convert ‘std::__cxx11::list<ssverbindungsdaten>’
to ‘std::__cxx11::list<ssverbindungsdaten>*’
in assignment
liSSVVerbinngsdaten = ss->holeAlleRohdaten();
The list holeAlleRohdaten
should be a lsit, containing all Classes ssverbindungsdaten
.
That I can later loop trough the liSSVVerbinngsdaten
in main.cpp
, and get all Classes ssverbindungsdaten
with the values (int id, string test)
.
Why can't I just assing the List liSSVVerbinngsdaten
to ss->holeAlleRohdaten
?
Upvotes: 0
Views: 6475
Reputation: 16449
You should do what the others say about pointers, smart pointers, naming, ...
But your error is
liSSVVerbinngsdaten = ss->holeAlleRohdaten();
liSSVVerbinngsdaten
is a pointer to a list and ss->holeAlleRohdaten()
returns a list. You can fix your program by returning a pointer
ssservice.h:
class ssverbindungsdaten
{
public:
ssverbindungsdaten();
~ssverbindungsdaten();
int id;
string test;
};
class softswitch{
public:
softswitch();
~softswitch();
list<ssverbindungsdaten>* holeAlleRohdaten();
};
ssservice.ccp:
list<ssverbindungsdaten>* softswitch::holeAlleRohdaten(){
//ssverbindungsdaten * ssv = new ssverbindungsdaten;
list<ssverbindungsdaten> *listTmp = new list<ssverbindungsdaten>;
return listTmp;
}
main.cpp:
int main(int argc, char *argv[])
{
//TODO: Mulithreading
std::cout << "Starte ...";
softswitch *ss = new softswitch;
list<ssverbindungsdaten> *liSSVVerbinngsdaten = new list<ssverbindungsdaten>();
delete liSSVVerbinngsdaten;
liSSVVerbinngsdaten = ss->holeAlleRohdaten();
delete softswitch;
delete liSSVVerbinngsdaten;
return 0;
}
This answer is only to understand the error. As the others said you should avoid raw pointers and use smart pointers. If you work with raw pointers you should remember that to every new
belongs a delete
.
Upvotes: 1
Reputation: 92271
If you skip all the new
and pointers, the code will be a lot simpler:
int main(int argc, char *argv[])
{
//TODO: Mulithreading
std::cout << "Starte ...";
softswitch ss;
list<ssverbindungsdaten> liSSVVerbinngsdaten = ss.holeAlleRohdaten();
return 0;
}
C++ is not at all like Java or C#. The list
class will handle all the heap allocations internally. You don't have to do that.
Upvotes: 0
Reputation: 68
list<ssverbindungsdaten> *listTmp = new list<ssverbindungsdaten>;
The "new" keyword in c++ actually allocates a new pointer, so you are manually allocating a pointer to a list of ssverbindungsdaten objects. If you want to create a default instance of the class or list just use
list<ssverbindungen> listTmp;
Edit: Also stop coding in german and uppercase your class names
Upvotes: 2