473,505 Members | 14,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problems with STL vector class

sw
Hi,

Is it possible to insert a class <vec> which has a vector<double>
member, into the vector<vec> veclist for e.g?

I've been getting compilation errors when trying to insert using the
vector method push_back() ->

c:\program files\microsoft visual studio\vc98\include\xutility(39) :
error C2679: binary '=' : no operator defined which takes a right-hand
operand of type 'const class vec' (or there is no acceptable
conversion)

c:\program files\microsoft visual studio\vc98\include\vector(170) : see
reference to function template instantiation 'void __cdecl
std::fill(class vec *,class vec *,const class vec &)' being compiled

c:\program files\microsoft visual studio\vc98\include\xmemory(34) :
error C2558: class 'vec' : no copy constructor available

c:\program files\microsoft visual studio\vc98\include\xmemory(66) : see
reference to function template instantiation 'void __cdecl
std::_Construct(class vec *,const class vec &)' being compiled
Error executing cl.exe.
I've already had an overloaded operator= for my vec class and i dont
really understand where the error lies in.

Here is my source code:

class <vec> header file

public:

vec();
vec(vec&);
vec(int, int); //dimension, vecid
vec(int, int, vector<double>); //dimension, vecid, coords
vec(int, int, vector<double>, vector<int>); //dimension, vecid,
coords, list of conected vecs
~vec();

void setvecid(int);
void setveccoord(vector<double>);
void setvecconnect(vector<int>);
void setvec(int, int, vector<double>, vector<int>);
void setvec(vec&);
int getdim();
int getvecid();
vector<double> getcoord(); //get the point coordinates
vector<int> getvecconnect(); //get the list of connected vecs
void addconnectvec(int); //add connected vec to the vecconnect list
void delconnectvec(int); //remove connected vec from list
bool searchvec(int); //search for vec in the vecconnect list
void sortvecconnect(); //sort the list in the

vec operator=(vec&);
In my main(), i declare a global variable:

vector<vec> globalvecs;

I get the compile error when i use the statement:

globalvecs.push_back(tmpvec);

inside the following block of code:

for(i = 0; i < numVec; i++)
{
vec tmpvec(dimension, i); //temp vec used to add to the global veclist
vector<double> tmpcoord; //vector to hold temp coordinates b4 setting

//tmpvec's coord

for(j = 0; j < dimension; j++)
{
read >> x; //where x is of type double
tmpcoord.push_back(x);
}
tmpvec.setveccoord(tmpcoord);

globalvecs.push_back(tmpvec);

}

Advance thanks for your replies!
Regards,

sw

Jul 23 '05 #1
11 6279
The signature for a copy constructor is:

vec(vec const &);

and not

vec(vec &);

Rade
Jul 23 '05 #2
sw wrote:
vec operator=(vec&);


vec& operator=(const vec&);

::A::

Jul 23 '05 #3
sw wrote:
Hi,

Is it possible to insert a class <vec> which has a vector<double>
member, into the vector<vec> veclist for e.g?
Sure.
I've been getting compilation errors when trying to insert using the
vector method push_back() ->

c:\program files\microsoft visual studio\vc98\include\xutility(39) :
error C2679: binary '=' : no operator defined which takes a right-hand
operand of type 'const class vec' (or there is no acceptable
conversion)

c:\program files\microsoft visual studio\vc98\include\vector(170) : see
reference to function template instantiation 'void __cdecl
std::fill(class vec *,class vec *,const class vec &)' being compiled

c:\program files\microsoft visual studio\vc98\include\xmemory(34) :
error C2558: class 'vec' : no copy constructor available

c:\program files\microsoft visual studio\vc98\include\xmemory(66) : see
reference to function template instantiation 'void __cdecl
std::_Construct(class vec *,const class vec &)' being compiled
Error executing cl.exe.
I've already had an overloaded operator= for my vec class and i dont
really understand where the error lies in.
You need to be const-correct.

Here is my source code:

class <vec> header file

public:

vec();
vec(vec&);
With this signature, you say that the copy constructor will change the
original. It shouldn't (and probably doesn't) do that, so make it const:

vec (const vec&);
Â*vec(int, int, vector<double>); //dimension, vecid, coords
Â*vec(int, int, vector<double>, vector<int>); //dimension, vecid, coords, list of conected vecs
You should consider changing those two into taking references to the
vectors. That way you'll save an additional copy of them including
their whole content. Same for the other functions that take vectors.
vec operator=(vec&);


Same as above. The compiler says it quite explicitly:
"no operator defined which takes a right-hand operand of type 'const
class vec'"

Your operator= signature suggests that it will modify the right-hand
side of the assignment (i.e. it is not const), and the vector wants
one that doesn't do that.

Another thing: Don't make operator= return by value. This means that it
will create another copy of the whole vec (using the copy constructor)
for generating the return value. Make it:

vec& operator=(const vec&);

or

const vec& operator=(const vec&);

Jul 23 '05 #4
sw
thanks for the replies! your explanation has made it much clearer -
havent touched c++ code in a while, gotten quite rusty :)

Jul 23 '05 #5
sw
I'm facing another problem with the statement

globalvecs.push_back(tmpvec);

I've made the changes suggested accordingly. Now, the push_back()
method of the vector doesnt seem to copy "<vec> tmpvec" properly into
globalvecs. The dimension and vecid are copied accordingly but the
vector<double> veccoord variable doesnt seem to be copied properly into
the globalvecs.

I've tested the tmpvec values before this statement and the coord
values do exist and are not null values.

My source codes are:

for(i = 0; i < numVec; i++)
{
vec tmpvec(dimension, i); //temp vec used to add to the global
veclist
vector<double> tmpcoord; //vector to hold temp coordinates b4
setting tmpvec's coord
for(j = 0; j < dimension; j++)
{
read >> x;
tmpcoord.push_back(x);
}

tmpvec.setveccoord(tmpcoord);
cout<<"tmpvec coord's:"<< (tmpvec.getcoord()).at(0) << " "
<< (tmpvec.getcoord()).at(1) << " "
<< (tmpvec.getcoord()).at(2) << " "
<< (tmpvec.getcoord()).at(3) << " " <<endl;
globalvecs.push_back(tmpvec);

vector<double> tmpcoord1 = (globalvecs.at(i)).getcoord();

cout<< "globalvecs[" << i << "]" << endl
<< "dimension:" << globalvecs[i].getdim() << endl
<< "vecid:" << globalvecs[i].getvecid() << endl
<< "coords: "
<< tmpcoord1[0] << ", "
<< tmpcoord1[1] << ", "
<< tmpcoord1[2] << ", "
<< tmpcoord1[3] <<endl;
}
The output i get is:

tmpvec coord's:1 1.5 0.2 0
globalvecs[0]
dimension:4
vecid:0
coords: -1.45682e+144, -1.45682e+144, -1.41454e+144, 1.2798e-307
tmpvec coord's:5 1.3 0.1 4.00171
globalvecs[1]
dimension:4
vecid:1
coords: -1.45682e+144, -1.45682e+144, -1.41454e+144, 1.2834e-307
tmpvec coord's:2.5 1.4 3 2.37949
globalvecs[2]
dimension:4
vecid:2
coords: -1.45682e+144, -1.45682e+144, -1.41454e+144, 1.2798e-307
tmpvec coord's:2.3 5 1.5 5
globalvecs[3]
dimension:4
vecid:3
coords: -1.45682e+144, -1.45682e+144, -1.41454e+144, 1.2798e-307
tmpvec coord's:2.4 0 1.5 0.806838
globalvecs[4]
dimension:4
vecid:4
coords: -1.45682e+144, -1.45682e+144, -1.41454e+144, 1.2798e-307
tmpvec coord's:2.5 2.5 1.5 1.95897
globalvecs[5]
dimension:4
vecid:5
coords: -1.45682e+144, -1.45682e+144, -1.41454e+144, 1.2798e-307
Press any key to continue

Jul 23 '05 #6
Rade wrote:
The signature for a copy constructor is:

vec(vec const &);

and not

vec(vec &);


Both are valid signatures for a copy constructor, but they still mean
different things.

Jul 23 '05 #7
sw wrote:
I'm facing another problem with the statement

globalvecs.push_back(tmpvec);

I've made the changes suggested accordingly. Now, the push_back()
method of the vector doesnt seem to copy "<vec> tmpvec" properly into
globalvecs. The dimension and vecid are copied accordingly but the
vector<double> veccoord variable doesnt seem to be copied properly into
the globalvecs.

I've tested the tmpvec values before this statement and the coord
values do exist and are not null values.


How are your operator= and copy constructor of your vec implemented? Do you
actually copy the vector within them?

Jul 23 '05 #8
sw
My operator= is implemented as:

const vec& vec::operator=(const vec &right)
{
dim = right.dim;
vecid = right.vecid;
setveccoord(right.coord);
setvecconnect(right.vecconnect);
return *this;
}

with the setveccoord and setvecconnect as:

void vec::setveccoord(vector<double> point)
{
coord.clear();

int size = point.size();
int i;
double x;
for(i = 0; i < size; i++)
{
x = point[i];
coord.push_back(x);
}
}

void vec::setvecconnect(vector<int> vecs)
{
vecconnect.clear();
int size = vecs.size();
int i;
int x;
for(i = 0; i < size; i++)
{
x = vecs[i];
vecconnect.push_back(x);
}
}

The operator= seems to work, i tried the assigment between 2 <vec>
variables and the coord values are copied accordingly.

Jul 23 '05 #9
> Both are valid signatures for a copy constructor, but they still mean
different things.


Right, but in > 99% cases of const-correct programs you need the signature
with 'const'. I think (half-seriously) that the other form of copy
constructor should be outlawed for all but C++ experts.

Seriously: I am just afraid that your response may make the OP believe that
the two signatures of the copy constructor are equal, while in fact they are
not (that is true even on the language level, i.e. the compiler will
generate only the 'const' form when no copy constructor (of any form) is
present).

Rade
Jul 23 '05 #10
sw
I omitted the copy constructor accidentally. Here it is now:

vec::vec(const vec &newvec)
{
dim = newvec.dim;
vecid = newvec.vecid;
setveccoord(coord);
setvecconnect(vecconnect);
}

Jul 23 '05 #11
sw wrote:
I omitted the copy constructor accidentally. Here it is now:

vec::vec(const vec &newvec)
{
dim = newvec.dim;
vecid = newvec.vecid;
setveccoord(coord);
setvecconnect(vecconnect);
}


Here we go. You probably meant:

setveccoord(newvec.coord);
setvecconnect(newvec.vecconnect);

Generally, you should prefer initialization over assignment in constructors.

vec::vec(const vec &newvec)
: dim(newvec.dim),
vecid(newvec.vecid),
coord(newvec.coord),
vecconnect(newvec.vecconnect)
{
}

Btw: Is that really all that your copy constructor is doing? If yes, you
should just remove it alltogether. If you don't define your own, the
compiler automatically generates one that does a memberwise copy, which is
what yours also does.

Jul 23 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2538
by: k0tic | last post by:
The intent of the following code is to call delete on each pointer in the vector dead implicitly via auto_ptr<T>'s reset method semantics which delete their current pointer before taking ownership...
3
1604
by: William Payne | last post by:
Consider this (templated) class member function: template<typename Type> void CircularContainer<Type>::insert(const Type& s) { vector<Type>::iterator itr = find(m_elements.begin(),...
16
2149
by: Honestmath | last post by:
Hi, I added the following line to my code within a class declaration: std::vector<Date> m_duedates(100); I also tried: std::vector<Date> m_duedates(100, Date());
4
1472
by: George Economos | last post by:
Hi all, I am using msvc 7.1 and have encountered the following code: ----------- A.hpp ----------- class A {
9
4591
by: Christian Chrismann | last post by:
Hi, I've a runtime problem with STL vectors. Here is the simplified version of the code: template <class Tclass A { ... private: vector<T*myvector; typename vector<T*>::itarator mIt;
3
9844
by: Nelis Franken | last post by:
Good day. I'm having trouble telling STL to use a member function to sort items within the relevant class. The isLess() function needs to have access to the private members of the Foo class to...
10
2692
by: Jess | last post by:
Hello, I have a program that stores dynamically created objects into a vector. #include<iostream> #include<vector> using namespace std;
13
2939
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
5
1610
by: Saltydog | last post by:
Ok so I ahve spent a while reading around this site and some others, and I thought there might of been some errors with my includes, but now I am just really confused. Basically I have a 3 classes, a...
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7303
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.