PDA

View Full Version : Trouble using c++ <map>



mkppk
December 4th, 2006, 04:28 AM
I'm a Python programmer learning to write an extension in C++. I've got a working extension, but it doesn't use any special C++ modules yet.. Tried to add a basic Map variable and started running into compilation errors. So,now I just trying to get a very basic C++ program that uses Map..

(I do not know a whole lot of C++, but want to use a data type similar to a Python dictionary. After some research I believe maps are the way to go, please correct me if I am wrong.)

Any ideas what I'm doing wrong or what is not setup correctly on my system (Ubuntu 6.06 LTS on a G3 PPC powerbook)?

Here is a full example of what I'm trying to do, this is /home/mkp/pytest/test.cpp:


#include <string>
#include <map>
#include <iostream>


main() {

map<string,int> count; string word;

while (cin >> word) count[word]++;

cout << count << endl;
}

Here is the compilation error:


mkp@mac:~/pytest$ gcc test.cpp
test.cpp: In function ‘int main()’:
test.cpp:8: error: ‘map’ was not declared in this scope
test.cpp:8: error: ‘string’ was not declared in this scope
test.cpp:8: error: expected primary-expression before ‘int’
test.cpp:8: error: expected `;' before ‘int’
test.cpp:8: error: expected `;' before ‘word’
test.cpp:10: error: ‘cin’ was not declared in this scope
test.cpp:10: error: ‘word’ was not declared in this scope
test.cpp:10: error: ‘count’ was not declared in this scope
test.cpp:12: error: ‘cout’ was not declared in this scope
test.cpp:12: error: ‘count’ was not declared in this scope
test.cpp:12: error: ‘endl’ was not declared in this scope
mkp@mac:~/pytest$

duff
December 4th, 2006, 04:32 AM
1) gcc is a C compiler, use g++ instead

2) just like you need to prefix modulename.attribute in Python, you need "std::" in front of map, string, cin, cout, and endl.

3) you can't send a map to cout like that.

Angry
December 4th, 2006, 08:54 PM
2) just like you need to prefix modulename.attribute in Python, you need "std::" in front of map, string, cin, cout, and endl.


Alternatively, you could type:


using namespace std;

after your header declarations. Saves on typing...