Solved

C++ - Why does my class constructor cause this error?  'Point3' : no copy constructor available or copy constructor is declared 'explicit'

Posted on 2006-10-29
4
278 Views
Last Modified: 2010-04-01
Hi,

I got this strange error the other day. When I isolated it, (made an empty project with nothing else in it), I found out that it only comes when using the following (Point3) class constructor.

a) If I comment out the constructor "Point3(Point3 &b) {...}" , then "myVector.push_back(p3);" works fine. Otherwise I get this error.

Im very puzzled why this is happening. Any ideas?

Thank you.

************ Main.cpp***********
#include <iostream>
#include <vector>
#include "Point3.h"

using namespace std;

void main(int argc, char** argv)
{
      vector<Point3> myVector;
      Point3 p3;
      myVector.push_back(p3);  
}
******************************
************ Point3.cpp***********
#ifndef      point3_h
#define point3_h

class Point3
{
      public:
            Point3(){ x = 0.0f;y = 0.0f;z = 0.0f;}                    // constructor 1
            // Point3(Point3 &b) {x = b.x; y=b.y; z=b.z;}    // constructor 2 - ONLY COMPILES IF THIS LINE IS COMMENTED OUT !!
            float x,y,z;
};

#endif
******************************
0
Comment
Question by:Gsteingr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Author Comment

by:Gsteingr
ID: 17828590

Sorry,

************ Point3.cpp***********

should be

************ Point3.h***********
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17828651
vector class internally uses copy constructor to create it's own copy of class instance. I confess that I don't understand completely C++ explicit constructors mumbo-jumbo, but at least I know simple fix for this problem:

    Point3(const Point3 &b)            // add const keyword
    {x = b.x; y=b.y; z=b.z;}

I know what to do, but I don't know why does this happen. Hopefully, somebody else can explain this :)
0
 

Author Comment

by:Gsteingr
ID: 17828722

Wow, it worked. Thanks a lot.

Does anybody know how the Vector is stored in memory. Would the Vector stored these two the same way:

Point3 p1;                                // Created on the Stack
Point3 *p2 = new Point3();       // Created on the Heap

myVector.push_back(p1);
myVector.push_back(*p3);
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 17828853
vector<T> stores objects of type T in continous memory block, like plain array. When new element is added and currect vector capacity is not enough, new array is allocated, all existing elements are copied to it, and new element is added. Old array is released.
vector creates it's own copy of T class, using copy constructor for this. In your sample, both for p1 and p2, vector creates new Point3 object - this is the same case for vector.
Simple types like int, char and simple structures like Point3 are usually kept in vector by value. Complex classes are usually kept by reference:

vector<ComplexClass*> v;
ComplexClass* p = new ComplexClass();
v.Add(p);

In this case, allocating and releasing of class instances is caller's responsibility. Vector only keeps class pointers which are actually 32-bit numbers. Caller allocates class instances on the heap, and releases them when element is removed from vector, or vector is cleared - vector doesn't do this itself.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question