0
class SmallVector
{
public:

  SmallVector(void);
  SmallVector( const int *tempArr, int arrSize );
  SmallVector( const SmallVector &tempObj );

  int size(void) const;
  int capacity(void) const;

  void push_back(int number); // ++
  void push_back(int *tempArr, int arrSize ); // ++
  int pop_back(void);

  int operator[](int index) const;
  SmallVector operator+(const SmallVector &tempObj);
  SmallVector operator*(int times);
  void printObj(void) const;

  bool isFull(void);

private:

  int staticIndex; // It refers to total element in the static part of the vector

  int dynamicIndex; // it refers to index number of dynamic array(it also refers to number of elements in dynamic section)
  int dynamicSize; // allocated memory for dynamic array

  int staticArray[32];
  int *dynamicArray;

  void expand(int newSize);
  void shrink(void);
};

SmallVector SmallVector::operator+(const SmallVector &tempObj)
{


     int i;

     int totalSize = ( this->size() + tempObj.size() );



     if( totalSize == 0 ) // arguments of sum operator are empty vector
         return SmallVector(); // default constructor is executed




     int *tempArray = new int[totalSize];

     for(i = 0; i < this->size(); ++i)// filling tempArray with first operand
     {
         if(i <= 31)
             tempArray[i] = staticArray[i];
         else
             tempArray[i] = dynamicArray[i - 32];
     }






     for(int j = 0; j < tempObj.size(); ++j, ++i)              
        tempArray[i] = tempObj[j];



     return SmallVector(tempArray, totalSize); // error is here     

}



 SmallVector::SmallVector( const SmallVector &tempObj )

 { // copy constructor



     staticIndex = 0; 

     dynamicIndex = 0;          
     dynamicSize = 0; 




     if( tempObj.size() > 32 ){

         dynamicSize = tempObj.size() - 32;
         dynamicArray = new int[dynamicSize];
     }





     for( int i = 0; i < tempObj.size(); ++i ){


          if( i <= 31 ){  // filling static array

             staticArray[staticIndex] = tempObj[i];
             ++staticIndex;

          }


          else{              

             dynamicArray[dynamicIndex] = tempObj[i];
             ++dynamicIndex;
          }
     }
 }

This is actually realizing a vector project ,but I encountered very interesting problem. If argument of copy constructor is not const, operator overloading + gives me an error when returning temporary instance of the class. I think, the compiler is confused about the constructors.

Error : no matching function for call to SmallVector::SmallVector(SmallVector&)

this error occurs in return statements in operator overloading +.

Cœur
  • 37,241
  • 25
  • 195
  • 267
gktg1414
  • 107
  • 2
  • 9
  • [Can't reproduce](https://godbolt.org/g/NjGBPv) – chris Mar 26 '17 at 00:53
  • _If argument of copy constructor is not const_ That's the thing.. Copy constructor always have a signature of `T (T const& other)`. If the reference argument is non-const - it's no longer a copy constructor. – Algirdas Preidžius Mar 26 '17 at 00:54
  • @AlgirdasPreidžius, `T(T&)` is permitted for a copy constructor. Not recommended, but permitted. Anyway, the error uses the argument rather than the parameter. – chris Mar 26 '17 at 01:15
  • Could you please remove all the useless code and explain a little bit more what exactly is your problem here? I'm a little confused. – Ludonope Mar 26 '17 at 01:34
  • The code you show is not the same code that you compile and that produces the error. The error mentions `SmallVector::SmallVector(SmallVector&)` constructor, but no such constructor exists in the code shown. – Igor Tandetnik Mar 26 '17 at 03:45
  • @chris Interesting. I always thought that signature of copy constructor was exact, up to a constness of arguments. I learned something today! – Algirdas Preidžius Mar 26 '17 at 10:42
  • I addad copy constructor and operator overloading +. I specified where the error occurs with single line comment in the code. – gktg1414 Mar 26 '17 at 11:19
  • The fact that deleting only const specifier in parameter of copy constructor causes an error is very bizarre – gktg1414 Mar 26 '17 at 11:21
  • Wow I missed the actual modification listed beneath the large code. You can't bind a temporary to a non-const reference. You'd have to make a copy from something that isn't a temporary. – chris Mar 26 '17 at 11:53
  • Sorry but I cannot understand chris. My operator + returns temporary object. While doing this, it is intended to use constructor with two arguments. However, the compiler tells me that there is an error and it points to copy constructor. How such a compiler cannot understand the difference between two different constructors. I passed two arguments ,then the compiler executes argument constructor not a copy constructor. – gktg1414 Mar 26 '17 at 12:10
  • I think this is c++ oop rule because I defined a class before ten mintes and I placed two constructors default constructor and copy constructor. My code gave an error and when I looked at the error, I encountered same result. I noticed that I missed const specifier. Thus, the compiler confused about default constructor and copy constructor. – gktg1414 Mar 26 '17 at 12:56
  • Are you sure that your error is about a `SmallVector&` and not a `SmallVector&&`? It would make a lot more sense. – Ludonope Mar 26 '17 at 13:33
  • Where do you free your temp array in operator+? In most cases it is a lot easier to implement operator+ in terms of operator += – Artemy Vysotsky Sep 24 '17 at 06:09

0 Answers0