For Programmers: Free Programming Magazines  


Home > Archive > VC STL > February 2006 > Replace a vector with a list









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Replace a vector with a list
OscarL

2006-02-21, 7:04 pm

I created a console application in Visual C++ 6 that uses the stl
vector container class. It compiles and runs fine, but when I change my

stl vector container to an stl list, (I only make three changes) I get
compiler errors. How can I fix this?

Here is my code, which I copied and pasted directly out of the IDE. I
probably didn't have to show everything here but I wasn't sure what
is and what isn't critical to solving my problem. Thanks!


//
---------------------------------------------------------------------------=
=AD---



// a.h: interface for the a class.
//
//////////////////////////////////////////////////////////////////////


#if !defined(AFX_A_H__4B01C16B_2A54_4749_874
C_0C4AFFE06002__INCLUDED_)
#define AFX_A_H__4B01C16B_2A54_4749_874C_0C4AFFE
06002__INCLUDED_


#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


#include <vector> // change this to include lists
#include <string>


class a
{
public:
a();
virtual ~a();


static std::vector<a> many_a; // change this to a list


static void add_ten_a();



};


#endif //
!defined(AFX_A_H__4B01C16B_2A54_4749_874
C_0C4AFFE06002__INCLUDED_)

//
---------------------------------------------------------------------------=
=AD---



// a.cpp: implementation of the a class.
//
//////////////////////////////////////////////////////////////////////


#include "stdafx.h"
#include "a.h"


using namespace std;


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


a::a()
{



}


a::~a()
{


}


vector<a> a::many_a; // change this to a list

void a::add_ten_a()
{
for (int i=3D0; i<10; i++)
{
a an_a;
many_a.push_back(an_a);
}



}


// --------------------------------------------------------------------

// test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include <iostream>
#include <string>


#include "a.h"


int main(int argc, char* argv[])
{


a::add_ten_a();


std::cout << "The size of my container is " << a::many_a.size()
<<
std::endl;


return 0;



}


// RESULT SHOULD BE: "The size of my container is 10"

--------------------Configuration: test1 - Win32
Release--------------------
Compiling...
StdAfx.cpp
Compiling...
test1.cpp
a=2Ecpp
Generating Code...
Linking...


test1.exe - 0 error(s), 0 warning(s)


----------------------------------------------------------------
Now I change my container class from a vector to a list (made changes
in 3 places)
and now i get these compiler errors:


--------------------Configuration: test1 - Win32
Release--------------------
Compiling...
test1.cpp
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\list(29) : error
C2079: '_Value' uses undefined class 'a'
C:\Documents and Settings\HP_Administrator\My Documents\NT
software\train_sw6\test1\a.h(26) : see reference to class template
instantiation 'std::list<class a,class std::allocator<class a> >' being

compiled
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(185) :

error C2079: 'value' uses undefined class 'a'
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\list(285)

: see reference to class template instantiation 'std::binder2nd<struct
std::not_equal_to<class a> >' being compiled
C:\Documents and Settings\HP_Administrator\My Documents\NT
software\train_sw6\test1\a.h(26) : see reference to class template
instantiation 'std::list<class a,class std::allocator<class a> >' being

compiled
a=2Ecpp
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\list(29) : error
C2079: '_Value' uses undefined class 'a'
C:\Documents and Settings\HP_Administrator\My Documents\NT
software\train_sw6\test1\a.h(26) : see reference to class template
instantiation 'std::list<class a,class std::allocator<class a> >' being

compiled
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(185) :

error C2079: 'value' uses undefined class 'a'
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\list(285)

: see reference to class template instantiation 'std::binder2nd<struct
std::not_equal_to<class a> >' being compiled
C:\Documents and Settings\HP_Administrator\My Documents\NT
software\train_sw6\test1\a.h(26) : see reference to class template
instantiation 'std::list<class a,class std::allocator<class a> >' being

compiled
Generating Code...
Error executing cl.exe.=20


test1.exe - 4 error(s), 0 warning(s)

Tom Widmer [VC++ MVP]

2006-02-21, 7:04 pm

OscarL wrote:
> I created a console application in Visual C++ 6 that uses the stl
> vector container class. It compiles and runs fine, but when I change my
>
> stl vector container to an stl list, (I only make three changes) I get
> compiler errors. How can I fix this?
>
> Here is my code, which I copied and pasted directly out of the IDE. I
> probably didn't have to show everything here but I wasn't sure what
> is and what isn't critical to solving my problem. Thanks!


> class a
> {
> public:
> a();
> virtual ~a();
>
>
> static std::vector<a> many_a; // change this to a list
>
>
> static void add_ten_a();
>
>
>
> };


The problem is that it is not legal to instantiate a std container with
an incomplete type (as 'a' is prior the the '};' above). It happens to
work for std::vector (you could call it an extension), but it doesn't
work for list, at least not with VC6. The easiest solution is to use either:

static std::list<a>* many_a;
or
static boost::shared_ptr<std::list<a> > many_a;

to prevent instantiation until a is complete.

Tom
OscarL

2006-02-21, 7:04 pm

Thanks Tom. I will change it to a pointer in my code. BTW I ran this
in my .Net compiler and there were no errors when I declared the list.
Only VC 6 detects the errors.

Carl Daniel [VC++ MVP]

2006-02-22, 3:58 am

OscarL wrote:
> Thanks Tom. I will change it to a pointer in my code. BTW I ran this
> in my .Net compiler and there were no errors when I declared the list.
> Only VC 6 detects the errors.


Whether it works depends on details of the standard library implementation
that are not covered by the standard. You just can't rely on being able to
instantiate std containers on incomplete types - it's simply not required to
work. That doesn't mean that it's required to fail - anywhere it does work
is technically an extension and not guaranteed to work on another
combination of compiler+standard library.

-cd


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com