Problems compiling

I am trying to follow along in a book I've been reading to learn C++ (C++ for dummies), and this code won't compile for me. Everything in it is exactly like the book has it (granted I can see typos here and there so that wouldn't surprise me in this case), and as far as I can tell (from my own abilities) it should compile. I am using Dev C++ with Mingw compiler on windows XP pro. Error messages are listed below the program's code.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <stdlib.h>
#include <list>
#include <string>

class Duck {
      public:
             string name;
             int weight;
             int length;
};

ostream& operator << (ostream &out, const Duck &duck) {
         cout << "(" << duck.name;
         cout << "," << duck.weight;
         cout << "," << duck.length;
         cout << ")";
         return out;
         }
void DumpDucks(list<Duck> *mylist) {
     list<Duck>::iternator iter = mylist->begin();
     while (iter != mylist->end()) {
           cout << *iter << endl;
           iter++
           }
     }
template <typename T>
list<T>::iterator MoveToPosition(list<T> *mylist, int pos) {
                  list<T>::iterator res = mylist->begin;
                  for (int loop = 1; loop <= pos; loop++) {
                      res++
                      }
                      return res;
                  }

using namespace std;

int main(int argc, char *argv[])
{
    list<Duck> Inarow;
    // Push some at beginning
    Duck d1 = {"Jim", 20, 15}; // Braces notation!
    Inarow.push_front(d1);
    Duck d2 = {"Sally", 15, 12};
    Inarow.push_front(d2);
    // Push some at end
    Duck d3 = {"Squakie", 18, 25};
    Inarow.push_front(d3);
    Duck d4 = {"Teumpeter", 19, 26};
    Inarow.push_front(d4);
    Duck d5 = {"Sneeky", 12, 13};
    Inarow.push_front(d5)
    cout << "===========" << endl;
    DumpDucks(&Inarow);
    
    // Reverse
    Inarow.reverse();
    cout << "===========" << endl;
    DumpDucks(&Inarow);
    // Splice
    // Need another list for this
    list<Duck> extras;
    Duck d6 = {"Grumpy", 8, 8};
    extras.puch_back(d6);
    Duck d7 = {"Sleepy", 8, 8};
    extras.puch_back(d7);
    Duck d8 = {"Ornery", 8, 8};
    extras.puch_back(d8);
    Duck d9 = {"Goofy", 8, 8};
    extras.puch_back(d9);
    cout << "===========" << endl;
    cout << "extras:" << endl;
    DumpDucks(&extras);
    list<Duck>::iterator first = 
        MoveToPosition(&extras, 1);
    list<Duck>::iterator last = 
        MoveToPosition(&extras, 3);
    list<Duck>::iterator into = 
        MoveToPosition(&Inarow, 2);
    Inarow.splice(into, extras, first, last);
    cout << "===========" << endl;
    cout << "extras after splice:" << endl;
    DumpDucks(&extras);
    cout << "===========" << endl;
    cout << "Inarow after splice:" << endl;
    DumpDucks(&Inarow);
    system("PAUSE");
    return EXIT_SUCCESS;
}

Error messages:
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
Compiler: Default compiler
Building Makefile: "C:\Cplusplus\Projects\Practice\Makefile.win"
Executing  make...
make.exe -f "C:\Cplusplus\Projects\Practice\Makefile.win" all
g++.exe -D__DEBUG__ -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include"    -g3

main.cpp:8: error: `string' does not name a type

main.cpp:13: error: expected constructor, destructor, or type conversion before '&' token
main.cpp:13: error: expected `,' or `;' before '&' token
main.cpp:20: error: variable or field `DumpDucks' declared void
main.cpp:20: error: `list' was not declared in this scope
main.cpp:20: error: expected primary-expression before '>' token
main.cpp:20: error: `mylist' was not declared in this scope

main.cpp:20: error: expected `,' or `;' before '{' token
main.cpp:28: error: expected constructor, destructor, or type conversion before '<' token
main.cpp:28: error: expected `;' before '<' token

main.cpp: In function `int main(int, char**)':

main.cpp:42: error: too many initializers for `Duck'
main.cpp:42: error: invalid conversion from `const char*' to `int'
main.cpp:44: error: too many initializers for `Duck'

main.cpp:44: error: invalid conversion from `const char*' to `int'
main.cpp:47: error: too many initializers for `Duck'
main.cpp:47: error: invalid conversion from `const char*' to `int'
main.cpp:49: error: too many initializers for `Duck'
main.cpp:49: error: invalid conversion from `const char*' to `int'
main.cpp:51: error: too many initializers for `Duck'
main.cpp:51: error: invalid conversion from `const char*' to `int'
main.cpp:53: error: expected `;' before "cout"
main.cpp:54: error: `DumpDucks' cannot be used as a function

main.cpp:59: error: `DumpDucks' cannot be used as a function
main.cpp:63: error: too many initializers for `Duck'
main.cpp:63: error: invalid conversion from `const char*' to `int'
main.cpp:64: error: 'class std::list<Duck, std::allocator<Duck> >' has no member named 'puch_back'
main.cpp:65: error: too many initializers for `Duck'
main.cpp:65: error: invalid conversion from `const char*' to `int'
main.cpp:66: error: 'class std::list<Duck, std::allocator<Duck> >' has no member named 'puch_back'
main.cpp:67: error: too many initializers for `Duck'

main.cpp:67: error: invalid conversion from `const char*' to `int'
main.cpp:68: error: 'class std::list<Duck, std::allocator<Duck> >' has no member named 'puch_back'
main.cpp:69: error: too many initializers for `Duck'
main.cpp:69: error: invalid conversion from `const char*' to `int'
main.cpp:70: error: 'class std::list<Duck, std::allocator<Duck> >' has no member named 'puch_back'
main.cpp:73: error: `DumpDucks' cannot be used as a function
main.cpp:75: error: `MoveToPosition' undeclared (first use this function)
main.cpp:75: error: (Each undeclared identifier is reported only once for each function it appears in.)
main.cpp:83: error: `DumpDucks' cannot be used as a function
main.cpp:86: error: `DumpDucks' cannot be used as a function

make.exe: *** [main.o] Error 1

Execution terminated
I think you have an old book.
Standard C++ names are in name space std. So everwhere where you use a standard name you shall specify the prefix std:: for example std::string name. Or you should use using directive using namespace std;.
Also standard C headers in C++ program shall have "c" prefix that is instead of

<stdlib.h>

shall be

<cstdlib>

So update your program as

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cstdlib>
#include <list>
#include <string>

using namespace std;

class Duck {
      public:
             string name;
             int weight;
             int length;
};
...
and so on.
Last edited on
You're trying to use something from the std namespace before you've specified that you're using the std namespace.
Thank you guys so much! That helped a lot, however I'm still getting some errors...

Updated code:
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <cstdlib>
#include <list>
#include <string>

using namespace std;

class Duck {
      public:
             string name;
             int weight;
             int length;
};

ostream& operator << (ostream &out, const Duck &duck) {
         cout << "(" << duck.name;
         cout << "," << duck.weight;
         cout << "," << duck.length;
         cout << ")";
         return out;
         }
void DumpDucks(list<Duck> *mylist) {
     list<Duck>::iterator iter = mylist->begin();
     while (iter != mylist->end()) {
           cout << *iter << endl;
           iter++;
           }
     }
template <typename T>
list<T>::iterator MoveToPosition(list<T> *mylist, int pos) {
                  list<T>::iterator res = mylist->begin;
                  for (int loop = 1; loop <= pos; loop++) {
                      res++;
                      }
                      return res;
                  }

int main(int argc, char *argv[])
{
    list<Duck> Inarow;
    // Push some at beginning
    Duck d1 = {"Jim", 20, 15}; // Braces notation!
    Inarow.push_front(d1);
    Duck d2 = {"Sally", 15, 12};
    Inarow.push_front(d2);
    // Push some at end
    Duck d3 = {"Squakie", 18, 25};
    Inarow.push_front(d3);
    Duck d4 = {"Teumpeter", 19, 26};
    Inarow.push_front(d4);
    Duck d5 = {"Sneeky", 12, 13};
    Inarow.push_front(d5);
    cout << "===========" << endl;
    DumpDucks(&Inarow);
    
    // Reverse
    Inarow.reverse();
    cout << "===========" << endl;
    DumpDucks(&Inarow);
    // Splice
    // Need another list for this
    list<Duck> extras;
    Duck d6 = {"Grumpy", 8, 8};
    extras.push_back(d6);
    Duck d7 = {"Sleepy", 8, 8};
    extras.push_back(d7);
    Duck d8 = {"Ornery", 8, 8};
    extras.push_back(d8);
    Duck d9 = {"Goofy", 8, 8};
    extras.push_back(d9);
    cout << "===========" << endl;
    cout << "extras:" << endl;
    DumpDucks(&extras);
    list<Duck>::iterator first = 
        MoveToPosition(&extras, 1);
    list<Duck>::iterator last = 
        MoveToPosition(&extras, 3);
    list<Duck>::iterator into = 
        MoveToPosition(&Inarow, 2);
    Inarow.splice(into, extras, first, last);
    cout << "===========" << endl;
    cout << "extras after splice:" << endl;
    DumpDucks(&extras);
    cout << "===========" << endl;
    cout << "Inarow after splice:" << endl;
    DumpDucks(&Inarow);
    system("PAUSE");
    return EXIT_SUCCESS;
}


Updated error report:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Compiler: Default compiler
Building Makefile: "C:\Cplusplus\Projects\Practice\Makefile.win"
Executing  make...
make.exe -f "C:\Cplusplus\Projects\Practice\Makefile.win" all
g++.exe -D__DEBUG__ -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include"    -g3

main.cpp:30: error: expected constructor, destructor, or type conversion before "MoveToPosition"
main.cpp:30: error: expected `;' before "MoveToPosition"

main.cpp: In function `int main(int, char**)':

main.cpp:75: error: `MoveToPosition' undeclared (first use this function)
main.cpp:75: error: (Each undeclared identifier is reported only once for each function it appears in.)

make.exe: *** [main.o] Error 1

Execution terminated 
Hmm.In Some compilers,You need to do:
cout <<"heh";
Try the following definition

1
2
3
4
5
6
7
8
template <typename T>
typename list<T>::iterator MoveToPosition(list<T> *mylist, int pos) {
                  typename list<T>::iterator res = mylist->begin;
                  for (int loop = 0; loop < pos; loop++) {
                      res++;
                      }
                      return res;
                  }
@Techy24, lol

@vlad from moscow, I tried what you said and got the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
Compiler: Default compiler
Building Makefile: "C:\Cplusplus\Projects\Practice\Makefile.win"
Executing  make...
make.exe -f "C:\Cplusplus\Projects\Practice\Makefile.win" all
g++.exe -D__DEBUG__ -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include"    -g3

main.cpp: In function `typename std::list<T, std::allocator<_CharT> >::iterator MoveToPosition(std::list<T, std::allocator<_CharT> >*, int) [with T = Duck]':
main.cpp:75:   instantiated from here
main.cpp:31: error: conversion from `<unknown type>' to non-scalar type `std::_List_iterator<Duck>' requested

make.exe: *** [main.o] Error 1

Execution terminated 


Actual code the above is trying to complie:

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <cstdlib>
#include <list>
#include <string>

using namespace std;

class Duck {
      public:
             string name;
             int weight;
             int length;
};

ostream& operator << (ostream &out, const Duck &duck) {
         cout << "(" << duck.name;
         cout << "," << duck.weight;
         cout << "," << duck.length;
         cout << ")";
         return out;
         }
void DumpDucks(list<Duck> *mylist) {
     list<Duck>::iterator iter = mylist->begin();
     while (iter != mylist->end()) {
           cout << *iter << endl;
           iter++;
           }
     }
template <typename T>
typename list<T>::iterator MoveToPosition(list<T> *mylist, int pos) {
                  typename list<T>::iterator res = mylist->begin;
                  for (int loop = 0; loop < pos; loop++) {
                      res++;
                      }
                      return res;
                  }

int main(int argc, char *argv[])
{
    list<Duck> Inarow;
    // Push some at beginning
    Duck d1 = {"Jim", 20, 15}; // Braces notation!
    Inarow.push_front(d1);
    Duck d2 = {"Sally", 15, 12};
    Inarow.push_front(d2);
    // Push some at end
    Duck d3 = {"Squakie", 18, 25};
    Inarow.push_front(d3);
    Duck d4 = {"Teumpeter", 19, 26};
    Inarow.push_front(d4);
    Duck d5 = {"Sneeky", 12, 13};
    Inarow.push_front(d5);
    cout << "===========" << endl;
    DumpDucks(&Inarow);
    
    // Reverse
    Inarow.reverse();
    cout << "===========" << endl;
    DumpDucks(&Inarow);
    // Splice
    // Need another list for this
    list<Duck> extras;
    Duck d6 = {"Grumpy", 8, 8};
    extras.push_back(d6);
    Duck d7 = {"Sleepy", 8, 8};
    extras.push_back(d7);
    Duck d8 = {"Ornery", 8, 8};
    extras.push_back(d8);
    Duck d9 = {"Goofy", 8, 8};
    extras.push_back(d9);
    cout << "===========" << endl;
    cout << "extras:" << endl;
    DumpDucks(&extras);
    list<Duck>::iterator first = 
        MoveToPosition(&extras, 1);
    list<Duck>::iterator last = 
        MoveToPosition(&extras, 3);
    list<Duck>::iterator into = 
        MoveToPosition(&Inarow, 2);
    Inarow.splice(into, extras, first, last);
    cout << "===========" << endl;
    cout << "extras after splice:" << endl;
    DumpDucks(&extras);
    cout << "===========" << endl;
    cout << "Inarow after splice:" << endl;
    DumpDucks(&Inarow);
    system("PAUSE");
    return EXIT_SUCCESS;
}
missing parenthesis mylist->begin()
I knew it had to be something simple lol. It's compiling just fine now.

Thanks to all who helped me with this. I really appreciate it!
Topic archived. No new replies allowed.