views:

44

answers:

1

I've been trying a program from codeproject, about ptr_vector, and while compiling, the above error is shown. Googling shows no hope to solve this problem. Could anyone here help out? Here's the entire code (am compiling with gcc 4.2.2)

#include <iostream>
#include <string>
#include <boost/ptr_container/ptr_vector.hpp>

using namespace std;    // for cout, endl, find, replace, ...
using namespace stdx;   // for ptr_vector, ptr_vector_owner
using namespace boost;

int main()
{
   cout << "---- ptr_vector demo ----" << endl;

   ptr_vector<string> ptv;
   ptr_vector_owner<string> owner (ptv);  // scope-guard: owner of new-ed objects

   ptv.push_back (new string ("Peter"));
   ptv.push_back (new string ("Paul"));
   ptv.insert    (ptv.end(), new string ("Margaret"));

   cout << " 1: " << ptv.front()  << " " << ptv.back() << endl;
   cout << " 2: " << ptv[1]       << " " << ptv.at(2)  << endl;
   cout << " 3: " << *ptv.begin() << " " << *(ptv.begin() + 1) << endl;

   cout << " 4:";
   for (ptr_vector<string>::iterator it = ptv.begin(); it != ptv.end(); ++it)
      cout << " " << *it;
   cout << endl;

   ptv.sort();
   cout << " 5: " << ptv[0] << " " << ptv[1] << " " << ptv[2] << endl;

   ptv.sort (greater<string>());
   cout << " 6: " << ptv[0] << " " << ptv[1] << " " << ptv[2] << endl;

   ptr_vector<string>::iterator iter;
   iter = find (ptv.begin(), ptv.end(), "Paul");
   if (iter != ptv.end())
      cout << " 7: " << *iter << endl;

   replace (ptv.begin(), ptv.end(), string ("Paul"), string ("Fred"));
   cout << " 8: " << ptv.begin()[1] << endl;

   string* str = ptv.pop_back();
   cout << " 9: " << *str <<  " - size: " << ptv.size() << endl;
   delete str;

   delete ptv.detach (ptv.begin());
   cout << "10: " << ptv[0] << " - size: " << ptv.size() << endl;

   ptr_vector<string> ptvTwo;
   ptr_vector_owner<string> ownerTwo (ptvTwo);

   ptvTwo.push_back (new string ("Elisabeth"));
   ptvTwo.push_back (new string ("Susan"));
   ptv.swap(ptvTwo);
   if (ptv < ptvTwo)
      cout << "11: " << *ptv.begin() << " - size: " << ptv.size() << endl;

   return 0;
}//main
+4  A: 

stdx is not a standard namespace, it is defined by the particular implementation you are trying to use. You are not using the header file include #include "ptr_vector.h" inside which namespace stdx exists. Currently the ptr_vector you are using is being included from boost namespce. That begs the question, if you can use boost why do you want use stdx namespace solution.

Naveen
Am not using ptr_vector.h because there is no such file in my system. So I'm using ptr_vector.hpp. Am I wrong to do so?
Nav
Yes, you need to download the source code from CodeProject. Is this the only error you are getting? Is the compiler not complaining about class such as `ptr_vector_owner`? I suggest you to either use the `boost` solution (preferred) or use the `stdx` solution, don't mixup both.
Naveen
Oh...ok. Thanks. I'll go with the boost solution. The error does show "‘ptr_vector_owner’ was not declared in this scope". Is there a way to solve the problem?
Nav
@Nav: That class is not needed when using the `boost` solution. The `boost::ptr_vector` class will take care of deleting the pointed to objects itself.
Naveen