0
因此,當我嘗試在Unix中運行我的頭文件時,出現錯誤提示「錯誤C2440:'正在初始化':無法從'std :: vector < _Ty> *'轉換爲'std :: vector < _Ty>'「。我想我因爲我在所有操作中調用的「變換」而出現此錯誤,但我不確定。這裏的頭文件:C++中的未知錯誤信息C2440
#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;
}
這在MSVS2008編譯罰款,但在ideone它沒有。奇怪的。 – 2012-02-12 17:45:27
這將有助於(a)如果你有一個較小的片段,並(b)告訴我們哪些行是特別抱怨的。 – Joe 2012-02-12 17:45:34
[三星程序員]的必須鏈接(http://c2.com/cgi/wiki?ThreeStarProgrammer) – 2012-02-12 17:47:50