473,563 Members | 2,895 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\inc lude\xutility(3 9) :
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\inc lude\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\inc lude\xmemory(34 ) :
error C2558: class 'vec' : no copy constructor available

c:\program files\microsoft visual studio\vc98\inc lude\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(vec tor<double>);
void setvecconnect(v ector<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(i nt); //add connected vec to the vecconnect list
void delconnectvec(i nt); //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(dimensio n, 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_b ack(x);
}
tmpvec.setvecco ord(tmpcoord);

globalvecs.push _back(tmpvec);

}

Advance thanks for your replies!
Regards,

sw

Jul 23 '05 #1
11 6288
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\inc lude\xutility(3 9) :
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\inc lude\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\inc lude\xmemory(34 ) :
error C2558: class 'vec' : no copy constructor available

c:\program files\microsoft visual studio\vc98\inc lude\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(dimensio n, 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_b ack(x);
}

tmpvec.setvecco ord(tmpcoord);
cout<<"tmpvec coord's:"<< (tmpvec.getcoor d()).at(0) << " "
<< (tmpvec.getcoor d()).at(1) << " "
<< (tmpvec.getcoor d()).at(2) << " "
<< (tmpvec.getcoor d()).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(rig ht.coord);
setvecconnect(r ight.vecconnect );
return *this;
}

with the setveccoord and setvecconnect as:

void vec::setveccoor d(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::setvecconn ect(vector<int> vecs)
{
vecconnect.clea r();
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

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

Similar topics

1
2543
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 of reset's arg in addition to when the object is destroyed -- in this case when, as an automatic functor variable, the auto_ptr goes out of scope....
3
1608
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(), m_elements.end(), s); // snip }
16
2158
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
1477
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
4598
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
9851
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 determine if the one item is less than the other (it uses two vectors, one containing the actual data, and one that stores IDs that index into the...
10
2700
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
2943
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
1617
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 main class and 2 sub classes the sub classes point at each other, but interactions is no more then pointing. In the main program I have #include...
0
7665
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7642
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2082
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 we have to send another system
0
924
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.