can you replace this line of code ?

A

asdf

Here is a line of code that I'm trying to replace to use std::vector:

Foo theFoos[MAX_ROWS][MAX_COLS];


I tried to use vectors with

std::vector< std::vector< Foo > > theFoos( MAX_ROWS, std::vector<Foo>(
MAX_COLS ) );

But the complier is giving me problems saying that copy ctor is private
for Foo ...

Sooooo .... what can I do to fix this with just changing 1 line of code
??
 
B

Ben Pope

asdf said:
Here is a line of code that I'm trying to replace to use std::vector:

Foo theFoos[MAX_ROWS][MAX_COLS];


I tried to use vectors with

std::vector< std::vector< Foo > > theFoos( MAX_ROWS, std::vector<Foo>(
MAX_COLS ) );

But the complier is giving me problems saying that copy ctor is private
for Foo ...

Well, then that is your problem. The value_type of vector must be copyable.
Sooooo .... what can I do to fix this with just changing 1 line of code
??

You cannot.

Ben Pope
 
A

Alan Johnson

asdf said:
Here is a line of code that I'm trying to replace to use std::vector:

Foo theFoos[MAX_ROWS][MAX_COLS];


I tried to use vectors with

std::vector< std::vector< Foo > > theFoos( MAX_ROWS, std::vector<Foo>(
MAX_COLS ) );

But the complier is giving me problems saying that copy ctor is private
for Foo ...

Sooooo .... what can I do to fix this with just changing 1 line of code
??

I doubt a one line fix exists. The problem is that the type Foo does
not meet the requirments for standard containers. From section 23.1.3
of the standard:

"The type of objects stored in these components must meet the
requirements of CopyConstructible types (20.1.3), and the additional
requirements of Assignable types."

You should give Foo a copy constructor and operator= function (if it
makes sense for type Foo to have them, and you have access to make those
changes). Alternatively, you could define a "handle" type that
dynamically allocates and manages a Foo, and which meets the
requirements for standard containers.

May I ask what additional functionality you need that is not provided by
your line:

Foo theFoos[MAX_ROWS][MAX_COLS];

While the rule of thumb is to prefer vectors over arrays, this seems
like a situation in which that wisdom may not apply, unless you actually
need some functionality of a vector.

Alan
 
I

Ian Collins

asdf said:
Here is a line of code that I'm trying to replace to use std::vector:

Foo theFoos[MAX_ROWS][MAX_COLS];


I tried to use vectors with

std::vector< std::vector< Foo > > theFoos( MAX_ROWS, std::vector<Foo>(
MAX_COLS ) );

But the complier is giving me problems saying that copy ctor is private
for Foo ...

Sooooo .... what can I do to fix this with just changing 1 line of code
??
Make the copy constructor public...
 
B

Ben Pope

Ian said:
asdf said:
Here is a line of code that I'm trying to replace to use std::vector:

Foo theFoos[MAX_ROWS][MAX_COLS];


I tried to use vectors with

std::vector< std::vector< Foo > > theFoos( MAX_ROWS, std::vector<Foo>(
MAX_COLS ) );

But the complier is giving me problems saying that copy ctor is private
for Foo ...

Sooooo .... what can I do to fix this with just changing 1 line of code
??
Make the copy constructor public...

....and possibly implement it. All on one line. :)

Ben Pope
 
A

asdf

The only reason I was doing this in the first place is that MAX_ROWS is
being changed to no longer be constant but only know at program
startup. The problem can be fixed by using new I believe. Something
like ( forgive my syntax ):

Foo (*theFoos)[MAX_COLS] = new Foo[MAX_ROWS][MAX_COLS];

( MAX_COLS is still constant )..
 
A

asdf

This is what I would normally do except the programmer of the Foo class
is a tyrant that does not take suggestions and refuses to change is
code.
 
B

Ben Pope

asdf said:
The only reason I was doing this in the first place is that MAX_ROWS is
being changed to no longer be constant but only know at program
startup. The problem can be fixed by using new I believe.

Yes.

Ben Pope
 
A

Alan Johnson

asdf said:
This is what I would normally do except the programmer of the Foo class
is a tyrant that does not take suggestions and refuses to change is
code.

You could do something like:

// Untested code.
class FooHandle
{
public:
Foo() : ptr(new Foo()) {}
Foo &get() { return *ptr ; }

private :
boost::shared_ptr<Foo> ptr ;
} ;


Then create a vector of vectors of FooHandles. This sidesteps the need
for Foo to have a copy constructor or assignment operator.

Alan
 
A

Alan Johnson

Alan said:
You could do something like:

// Untested code.
class FooHandle
{
public:
Foo() : ptr(new Foo()) {}
Foo &get() { return *ptr ; }

private :
boost::shared_ptr<Foo> ptr ;
} ;


Then create a vector of vectors of FooHandles. This sidesteps the need
for Foo to have a copy constructor or assignment operator.

Alan

I should really think before I post. That would create a single Foo and
make MAX_ROWS*MAX_COLS FooHandle's pointing to it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top