简体   繁体   中英

Unknown error message C2440 in C++

So when I try to run my header file in Unix, I get an error saying "error C2440: 'initializing' : cannot convert from 'std::vector<_Ty> *' to 'std::vector<_Ty>'". I think I'm getting this error because of the "transform" I'm calling in allOperations, but I'm not sure. Here's the header file:

#include <iostream>
#include <vector>
#include <string>
#include <fstream> //library for files
#include <cctype>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <typeinfo>


using namespace std;

template <class T>
void alloc3DArray(T *** &x, int numberOfRows, int numberOfColumns,int numberOfDepth  )
{

 int i=0;
 int j=0;
 int k=0;


 // allocate an array for array of arrays
  x = new T ** [numberOfRows];


 // Allocate an array for each element of the first array
   for(i = 0; i < numberOfRows; i++)
    {

        x[i] = new T *[numberOfColumns];

  // Allocate an array of T for each element of this array
        for(j = 0; j < numberOfColumns; j++)
        {

            x[i][j] = new T [numberOfDepth];

   // Specify an initial value 
   for(int k = 0; k < numberOfDepth; ++k)
   {
    x[i][j][k] = -1;


   }


        }
    }

}

template <class T>
void dealloc3DArray(T *** &x, int numberOfRows, int numberOfColumns )

{
 // Check if it exeists 
 if(!x) //it does not exist
         exit(1);



    for (int i = 0; i < numberOfRows; ++i) 
    {
   for (int j = 0; j < numberOfColumns; ++j)

        {
    //delete innest
            delete [] x[i][j]; 
        }

   //delete columns
        delete [] x[i];
    }
 //delete first array
    delete [] x;
}

template <class T>
const vector<T>  allOperationsNotWorking(T *** &myArray1, T *** &myArray2,  T *** &myArray3,int numberOfRows, int numberOfColumns,int numberOfDepth  )

{
    int i=0;
 int j=0;
 int k=0;

    int size = numberOfRows * numberOfColumns * numberOfDepth; //find vector size
    vector<T> &myvector = vector<T>(size);// create a vector
    //vector<T>::const_iterator it;// const_iterator is faster than iterator
    for(i = 0; i < numberOfRows; i++)
   for(j = 0; j < numberOfColumns; j++)
    for(k = 0; k < numberOfDepth; k++){

     myArray3[i][j][k] =myArray1[i][j][k]+myArray2[i][j][k];

     myvector.push_back(myArray3[i][j][k]);


    }
     return myvector;


}

vector<string>  allOperationsString(string *** &myArray1, string *** &myArray2,  string *** &myArray3,int numberOfRows, int numberOfColumns,int numberOfDepth  )

{
    int i=0;
 int j=0;
 int k=0;

    int size = numberOfRows * numberOfColumns * numberOfDepth; //find vector size
    vector<string> myvector = vector<string>(size);// create a vector
    //vector<T>::const_iterator it;// const_iterator is faster than iterator
    for(i = 0; i < numberOfRows; i++)
   for(j = 0; j < numberOfColumns; j++)
    for(k = 0; k < numberOfDepth; k++){

     myArray3[i][j][k] =myArray1[i][j][k]+myArray2[i][j][k];

     myvector.push_back(myArray3[i][j][k]);


    }
     return myvector;


}

template <class T>
vector<T> isWord(vector<T> &stringVector, vector<T> &goodOnes,vector<T> &badOnes, vector<T> &dict)
{
    vector<int> strV;


 if (typeid(stringVector).name()== typeid(strV).name()){

 //if (typeid(stringVector)==int){
  ofstream badFile; //  declare and object as output file  using ofstream
  cout << " Illegal Vector" << endl;
  badFile.open ("hw1bout2.txt", ios::app);
  badFile << "Illegal Vector" << endl;
  badFile.close();
  return badOnes;
  }

 int i=0;
 int j=0;
 int FIRSTSIZE=stringVector.size();
 int SECONDSIZE = dict.size();
 std::string sInput="";
 std::string sDict="";

 sort(stringVector.begin(), stringVector.end());//sort() uses quicksort from the algorith library

  for(int i=0, j=0; i < FIRSTSIZE && j < SECONDSIZE; ){
   sInput=stringVector[i];


   std::transform(sInput.begin(), sInput.end(), sInput.begin(), std::toupper); //convert input word to upper

   sDict=dict[j];


   if(sInput.compare(sDict) == 0) {
   goodOnes.push_back(stringVector[i]); //write good word to vector

   i++;//advance one position in string vector
   j++;//advance one position in dictionary
    }

  else if(stringVector[i] < dict[j] && FIRSTSIZE < SECONDSIZE){// wrods did not match
   if (stringVector[i].size() >0) badOnes.push_back(stringVector[i]); //write bad word to vector if not empty string
   i++;//advance one position in string vector
   }

  else{
   j++;//advance one position in dictionary
    }
}



 return goodOnes;
}

template<class T >    
vector<string> isWord(vector<int> &stringVector, vector<string> &goodOnes,vector<string> &badOnes, vector<string> &dict)
{

 vector<int> strV;


 if (typeid(stringVector).name()== typeid(strV).name()){

  badOnes.push_back("Illegal Vector" );
 }
  return badOnes;

}

//template<class T>
vector<int>  allOperations(int *** &myArray1, int *** &myArray2,  int *** &myArray3,int numberOfRows, int numberOfColumns,int numberOfDepth  )

{
    int i=0;
 int j=0;
 int k=0;

    int size = numberOfRows * numberOfColumns * numberOfDepth; //find vector size
    vector<int> myvector = vector<int>(size);// create a vector
    //const vector<int>& myvector;
    //vector<int>::const_iterator it = myvector.begin();
    vector<int>::const_iterator it;// const_iterator is faster than iterator
    for(i = 0; i < numberOfRows; i++)
   for(j = 0; j < numberOfColumns; j++)
    for(k = 0; k < numberOfDepth; k++){

     myArray3[i][j][k] =myArray1[i][j][k]+myArray2[i][j][k];

     myvector.push_back(myArray3[i][j][k]);


    }
     return myvector;


}

The actual error message gotten from g++, which is not the one you posted, pretty clearly indicates that the last parameter to std::transform is the name of an overloaded function.

Since that parameter is used for template argument deduction, the compiler has no way to select any one of the overloads. We'll help it by naming the collection of overloaded functions in a strongly-typed context:

char (*transformation_fn)(char) = &std::toupper;
std::transform(sInput.begin(), sInput.end(), sInput.begin(), transformation_fn);

With this change, the code compiles without errors .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM