Strange namespace-related compile error?

Started by
2 comments, last by noerrorsfound 15 years, 11 months ago
This is an unedited example from chapter 6 of Beginning C++ Through Game Programming:

// Inventory Displayer
// Demonstrates constant references

#include <iostream>
#include <string>
#include <vector>

using namespace std;

//parameter vec is a constant reference to a vector of strings
void display(const vector<string>& inventory);

int main()
{
    vector<string> inventory;
    inventory.push_back("sword");
    inventory.push_back("armor");
    inventory.push_back("shield");  
    
    display(inventory);

    return 0;
}

//parameter vec is a constant reference to a vector of strings
void display(const vector<string>& vec)
{
    cout << "Your items:\n";
    for (vector<string>::const_iterator iter = vec.begin(); 
         iter != vec.end(); ++iter)
         cout << *iter << endl;
}



However, my edited version (no using directive) does not:

// Inventory Displayer
// Demonstrates constant references

#include <iostream>
#include <string>
#include <vector>

//parameter vec is a constant reference to a vector of strings
void display(const vector<std::string>& inventory);

int main()
{
    vector<std::string> inventory;
    inventory.push_back("sword");
    inventory.push_back("armor");
    inventory.push_back("shield");

    display(inventory);

    return 0;
}

//parameter vec is a constant reference to a vector of strings
void display(const vector<std::string>& vec)
{
    std::cout << "Your items:\n";
    for (vector<std::string>::const_iterator iter = vec.begin();
         iter != vec.end(); ++iter)
         std::cout << *iter << std::endl;
}



What obvious mistake am I not seeing?
Advertisement
vector (in addition to string) belongs to the std namespace. By omitting the using directive the compiler can no longer locate vector.
std::vector
Quote:Original post by fpsgamer
vector (in addition to string) belongs to the std namespace. By omitting the using directive the compiler can no longer locate vector.

Quote:Original post by Ernasty
std::vector

*walks away in shame

This topic is closed to new replies.

Advertisement