![](/img/trans.png)
[英]How do I add a string to my array of type struct which contains both strings and ints in c++?
[英]How do I store strings (or any type) into an array of type T?
我試圖在 T 類型的數組中存儲一個字符串(或任何類型),但出現兩個錯誤:
“std::string &”類型的引用(非const限定)不能用“const char [3]”類型的值初始化
“bool container::insertBack(T &)”:無法將參數 1 從“const char [3]”轉換為“T &”
我嘗試將類型更改為 int 而不是 string:我收到了類似的錯誤消息。
#include <iostream>
#include <string>
template<typename T>
class container
{
public:
container();
// Postcondition: data member n is initialized to -1 and all elements in the empty arr array are initialized to zero
bool isFull();
// Postcondition: returns true if the container object (i.e., the arr array) is full; returns false otherwise
bool insertBack(T& val);
// Precondition: the container object is not full
// Postcondition: if arr array is not full, n is incremented by 1; returns true with val is inserted at the end of the arr array
// Otherwise, returns false; the value is not inserted and program execution continues.
private:
static const int CAPACITY = 10; // physical size of the arr array or the storage capacity of a container object
T arr[CAPACITY]; // arr array can store up to CAPACITY (10 in our case) of any type
int n; // n is used as the subscript for the arr array. n is initialized to -1 for an empty array
// Each time a new value is inserted into the arr array, n must first be incremented
// by 1. Since n has been initialized to -1, the first inserted value is stored in arr[0],
// and the 2nd inserted value will be in arr[1], etc. and the nth inserted value will be
// stored in arr[n – 1]. Obviously, n + 1 represents the actual number of elements
// stored in the array after n rounds of insertion.
};
template<typename T>
container<T>::container()
{
n = -1;
T arr[CAPACITY] = { 0 };
}
template<typename T>
bool container<T>::isFull()
{
return n == CAPACITY - 1;
}
template<typename T>
bool container<T>::insertBack(T& val)
{
if (!isFull())
{
n++;
arr[n - 1] = val;
return 1;
}
else
{
return 0;
}
}
int main()
{
container<std::string> s1;
s1.insertBack("aa");
}
對於相同的錯誤, g++
給出了略有不同的輸出:
cannot bind non-const lvalue reference of type 'std::__cxx11::basic_string<char>&' to an rvalue of type 'std::__cxx11::basic_string<char>'
clang++
:
non-const lvalue reference to type 'std::__cxx11::basic_string<char>' cannot bind to a value of unrelated type 'const char [3]'
解決方案是將參數作為const&
。
接下來你會發現T arr[CAPACITY] = { 0 };
給出一個運行時異常,如: basic_string::_M_construct null not valid
。
你不是零初始化arr
那樣。 事實上,您正在創建一個新的arr
並嘗試使用nullptr
構造它,這不適用於std::string[]
。
您也可以像標准容器一樣使用無符號整數作為size_t
來計算元素,以便將來與標准函數/算法的交互更容易。
有了這個固定::
#include <iostream>
#include <string>
template<typename T, size_t Capacity = 10> // made the capacity selectable
class container {
public:
container() : arr{}, n(0) {} // using member initializer list
bool isFull() const { return n == Capacity; } // correct test with a 0 based counter
bool insertBack(const T& val) { // const
if (isFull()) return false;
// return true or false, not 1 or 0
arr[n++] = val;
return true;
}
private:
T arr[Capacity];
size_t n;
};
int main() {
container<std::string> s1;
s1.insertBack("aa");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.