[英]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>'". 因此,当我尝试在Unix中运行头文件时,出现错误消息“错误C2440:'初始化':无法从'std :: vector <_Ty> *'转换为'std :: vector <_Ty>'”。 I think I'm getting this error because of the "transform" I'm calling in allOperations, but I'm not sure.
我想由于在allOperations中调用了“转换”而收到此错误,但是我不确定。 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. 从g ++(不是您发布的错误消息)获得的实际错误消息非常清楚地表明,
std::transform
的最后一个参数是重载函数的名称。
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 . 进行此更改后, 代码可以正确编译 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.