gpt4 book ai didi

c++ - C++ 中的未知错误消息 C2440

转载 作者:行者123 更新时间:2023-11-28 08:10:26 25 4
gpt4 key购买 nike

所以当我尝试在 Unix 中运行我的头文件时,我收到一条错误消息“error C2440: 'initializing' : cannot convert from 'std::vector<_Ty> *' to 'std::vector<_Ty> '”。我想我收到这个错误是因为我在 allOperations 中调用了“转换”,但我不确定。这是头文件:

#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;


}

最佳答案

从 g++ 获得的实际错误消息(不是您发布的错误消息)非常清楚地表明 std::transform 的最后一个参数是重载函数的名称。

由于该参数用于模板参数推导,编译器无法选择任何一个重载。我们将通过在强类型上下文中命名重载函数的集合来帮助它:

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

通过此更改,the code compiles without errors .

关于c++ - C++ 中的未知错误消息 C2440,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9250995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com