Problem with stl list
May 17, 2013 at 12:44pm May 17, 2013 at 12:44pm UTC
I made a little program to parse string, but my compiler gives me a mistake. And I don't know what have I done wrong. Please help!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <iostream>
#include <string>
#include <list>
using namespace std;
class A
{
public :
list<string> ll;
A();
virtual ~A();
};
A::A()
{
ll = new list<string>;
}
A::~A()
{
delete ll;
}
int main()
{
A myList;
string str = "1+2+3" ;
for (int i = 0; i<5; i++)
{
if (is digit(str[i]))
{
myList.ll.push_back(i); //compiler gives a mistake here
}
}
return 0;
}
May 17, 2013 at 12:54pm May 17, 2013 at 12:54pm UTC
C++ is neither C# nor Java. So this syntax
std::list<std::string> ll = new std::list<std::string>;
is invalid in C++.
By the way we are not compilers that to search errors in your code. If your compiler issued an error then post it here.
May 17, 2013 at 1:21pm May 17, 2013 at 1:21pm UTC
As I understood my constructor ll = new list<string>;
is wrong... Could somebody explain how it should be?
May 17, 2013 at 1:23pm May 17, 2013 at 1:23pm UTC
std::list<std::string> ll; //done, list created
May 17, 2013 at 1:29pm May 17, 2013 at 1:29pm UTC
When an object of your class is being created then constructors of its members are called. Class std::list has a default constructor that will be called.
Topic archived. No new replies allowed.