无法将参数'int'转换为'char&'(Cannot convert argument 'int' to 'char &

系统教程 行业动态 更新时间:2024-06-14 16:57:17
无法将参数'int'转换为'char&'(Cannot convert argument 'int' to 'char &')

我收到一个我不理解的编译器错误,并且不知道如何正确处理它。 如果有人能就如何解决问题向我提出一些建议,我将不胜感激。 错误如下:

Error 1 error C2664: 'char myVector<char>::at(T &) const' : cannot convert argument 1 from 'int' to 'char &'

它位于标题代码的第159行,它读out << rho.at(n); 。

司机:

#include <iostream> #include "vectorHeader.h" using namespace std; int main() { const char START = 'A'; const int MAX = 12; // create a vector of doubles myVector<char> vectD; // push some values into the vector for (int i = 0; i < MAX; i++) { vectD.push_back(START + i); } // remove the last element vectD.pop_back(); // add another value vectD.push_back('Z'); // test memory management myVector<char> vectD2 = vectD; // display the contents cout << "\n["; for (int i = 0; i < vectD2.size() - 1; i++) { cout << vectD2[i] << ", "; } cout << "..., " << vectD2.last() << "]\n"; system("PAUSE"); return 0; }

标题:

#include <iostream> #include <iomanip> #include <string> #include <vector> #include <fstream> #include <stdexcept> //Declaring constant const int VECTOR_CAP = 2; template <class T> class myVector { private: //Setting data members T* vectorData; int cap; int numElements; public: //Default constructor //Purpose: Creates a vector //Parameters: None //Returns: None myVector(); //Parameterized constructor //Purpose: Creates a vector capacity of n //Parameters: None //Returns: None myVector(const T&); //Copy Constructor //Purpose: Copy data into vector //Parameters: myVector object //Returns: None myVector(const myVector& copy) { numElements = copy.numElements; vectorData = new T [numElements]; for (int i = 0; i < numElements; i++) { this->vectorData[i] = copy.vectorData[i]; } } //Destructor //Purpose:Deletes any dynamically allocated storage //Parameters: None //Returns: None ~myVector(); //Size function //Purpose: returns the size of your vector //Parameters: None //Returns: The size of your vector as an integer int size() const; //Capacity function //Purpose: Returns the capacity of the vector //Parameters: None //Returns: Maximum value that your vector can hold int capacity() const; //Clear function //Purpose: Deletes all of the elements from the vector and resets its size to zero // and its capacity to two; thus becoming empty //Parameters: None //Returns: None void clear(); //push_back function //Purpose: Adds the integer value n to the end of the vector //Parameters: Takes a integer to be placed in the vector //Returns: None void push_back(const T& n) { //If statement to handle if array is full if (numElements == cap) { //Doubling the capacity cap = cap * VECTOR_CAP; //Allocating new array T* newVectorData = new T[cap]; //Copying data for (int i = 0; i < numElements; i++) newVectorData[i] = vectorData[i]; //Deleting previous data delete[] vectorData; //Pointing to new data vectorData = newVectorData; } //Storing data vectorData[numElements++] = n; } //at function //Purpose: Returns the value of the element at position n in the vector //Parameters: None //Returns: Returns your current place with the vector T at(T&) const; //assignment //Purpose: Overload the = operator //Parameters: The two myVector objects we want to assign //Returns: The assignment myVector operator=(const myVector&); void pop_back(); int last(); myVector operator[](const myVector&); }; //Independant Functions template <typename T> int myVector<T>::capacity() const { return cap; } template <typename T> myVector<T> myVector<T>::operator=(const myVector& rho) { //Test for assingment if (this == &rho) { return *this; } //Delete lho delete[] this->vectorData; //Creating new array to fit rho data cap = rho.cap; this->vectorData = new int[cap]; //Copying data for (int i = 0; i < numElements; i++) { this->vectorData[i] = rho.vectorData[i]; } //Returning myVector object return *this; } template <typename T> std::ostream& operator<<(std::ostream& out, const myVector<T>& rho) { for (int n = 0; n < rho.size(); n++) { out << rho.at(n); } return out; }

I am receiving a compiler error that I don't understand, and don't know how to deal with it properly. If anyone could offer me some advice on how to fix it I would be much appreciated. The error reads:

Error 1 error C2664: 'char myVector<char>::at(T &) const' : cannot convert argument 1 from 'int' to 'char &'

It's on line 159 of the header code where it reads out << rho.at(n);.

Driver:

#include <iostream> #include "vectorHeader.h" using namespace std; int main() { const char START = 'A'; const int MAX = 12; // create a vector of doubles myVector<char> vectD; // push some values into the vector for (int i = 0; i < MAX; i++) { vectD.push_back(START + i); } // remove the last element vectD.pop_back(); // add another value vectD.push_back('Z'); // test memory management myVector<char> vectD2 = vectD; // display the contents cout << "\n["; for (int i = 0; i < vectD2.size() - 1; i++) { cout << vectD2[i] << ", "; } cout << "..., " << vectD2.last() << "]\n"; system("PAUSE"); return 0; }

Header:

#include <iostream> #include <iomanip> #include <string> #include <vector> #include <fstream> #include <stdexcept> //Declaring constant const int VECTOR_CAP = 2; template <class T> class myVector { private: //Setting data members T* vectorData; int cap; int numElements; public: //Default constructor //Purpose: Creates a vector //Parameters: None //Returns: None myVector(); //Parameterized constructor //Purpose: Creates a vector capacity of n //Parameters: None //Returns: None myVector(const T&); //Copy Constructor //Purpose: Copy data into vector //Parameters: myVector object //Returns: None myVector(const myVector& copy) { numElements = copy.numElements; vectorData = new T [numElements]; for (int i = 0; i < numElements; i++) { this->vectorData[i] = copy.vectorData[i]; } } //Destructor //Purpose:Deletes any dynamically allocated storage //Parameters: None //Returns: None ~myVector(); //Size function //Purpose: returns the size of your vector //Parameters: None //Returns: The size of your vector as an integer int size() const; //Capacity function //Purpose: Returns the capacity of the vector //Parameters: None //Returns: Maximum value that your vector can hold int capacity() const; //Clear function //Purpose: Deletes all of the elements from the vector and resets its size to zero // and its capacity to two; thus becoming empty //Parameters: None //Returns: None void clear(); //push_back function //Purpose: Adds the integer value n to the end of the vector //Parameters: Takes a integer to be placed in the vector //Returns: None void push_back(const T& n) { //If statement to handle if array is full if (numElements == cap) { //Doubling the capacity cap = cap * VECTOR_CAP; //Allocating new array T* newVectorData = new T[cap]; //Copying data for (int i = 0; i < numElements; i++) newVectorData[i] = vectorData[i]; //Deleting previous data delete[] vectorData; //Pointing to new data vectorData = newVectorData; } //Storing data vectorData[numElements++] = n; } //at function //Purpose: Returns the value of the element at position n in the vector //Parameters: None //Returns: Returns your current place with the vector T at(T&) const; //assignment //Purpose: Overload the = operator //Parameters: The two myVector objects we want to assign //Returns: The assignment myVector operator=(const myVector&); void pop_back(); int last(); myVector operator[](const myVector&); }; //Independant Functions template <typename T> int myVector<T>::capacity() const { return cap; } template <typename T> myVector<T> myVector<T>::operator=(const myVector& rho) { //Test for assingment if (this == &rho) { return *this; } //Delete lho delete[] this->vectorData; //Creating new array to fit rho data cap = rho.cap; this->vectorData = new int[cap]; //Copying data for (int i = 0; i < numElements; i++) { this->vectorData[i] = rho.vectorData[i]; } //Returning myVector object return *this; } template <typename T> std::ostream& operator<<(std::ostream& out, const myVector<T>& rho) { for (int n = 0; n < rho.size(); n++) { out << rho.at(n); } return out; }

最满意答案

正如它在锡上所说的那样。 当你有

T at(T&) const;

在myVector<char> ,转换为

char at(char&) const;

并且int不能绑定到对char的引用。 我想你想说

T at(int) const;

或更好,

T at(std::size_t) const;

因为std::size_t通常(按惯例)用于此类事情。 int可以隐式转换为std::size_t ,因此也可以正常工作。

Exactly what it says on the tin. When you have

T at(T&) const;

in a myVector<char>, that translates to

char at(char&) const;

and an int cannot be bound to a reference to char. I think you meant to say

T at(int) const;

or, better,

T at(std::size_t) const;

because std::size_t is usually (by convention) used for this sort of thing. int is implicitly convertible to std::size_t, so that will also just work.

更多推荐

本文发布于:2023-04-12 20:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/01d837b9833b9012ccf29af299f88e47.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   参数   int   argument   char

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!