Jump to content
  • Advertisement
Sign in to follow this  
noerrorsfound

Strange namespace-related compile error?

This topic is 3779 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
vector (in addition to string) belongs to the std namespace. By omitting the using directive the compiler can no longer locate vector.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!