看起來屬性test aisbn
正在成功存儲調用setCode()
,setDigit()
的數據。但麻煩開始出現問題,而我試圖將這些值存儲到list<test> simul
未能將值存儲到列表中
list屬性setDigit()
但代碼後需要數字的值。我怎樣才能把代碼和數字放到列表屬性中?我看不出問題在哪裏。代碼:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <list>
using namespace std;
class test
{
private:
string code;
int digit;
public:
//constructor
test(): code(""), digit(0) { }
//copy constructor
test(const test &other):
digit(other.digit)
{
for(unsigned int i=0; i < code.length(); i++)
code[i] = other.code[i];
}
//set up the private values
void setCode(const string &temp, const int num);
void setCode(const string &temp);
void setDigit(const int &num);
//return the value of the pointer character
const string &getCode() const;
const unsigned int getDigit() const;
};
const string& test::getCode() const
{
return code;
}
const unsigned int test::getDigit() const
{
return digit;
}
void test::setCode(const string &temp, const int num)
{
if((int)code.size() <= num)
{
code.resize(num+1);
}
code[num] = temp[num];
}
void test::setCode(const string &temp)
{
code = temp;
}
void test::setDigit(const int &num)
{
digit = num;
}
int main()
{
const string contents = "dfskr-123";
test aisbn;
list<test> simul;
list<test>::iterator testitr;
testitr = simul.begin();
int count = 0;
cout << contents << '\n';
for(int i=0; i < (int)contents.length(); i++)
{
aisbn.setCode(contents);
aisbn.setDigit(count+1);
simul.push_back(aisbn);
count++;
}
cout << contents << '\n';
/*for(; testitr !=simul.end(); simul++)
{
cout << testitr->getCode() << "\n";
}*/
}
忘記for循環。我想知道的是,simul.push_back(aisbn)只取得setDigit()中數字的值。你可以看看到用於代碼(INT I = 0;我<(int)的contents.length();我++) \t \t { \t \t \t aisbn.setCode(內容); \t \t \t aisbn.setDigit(count + 1); \t \t \t simul.push_back(aisbn); \t \t \t count ++; \t \t} – user2282596
@ user2282596修改答案 –
很多thx。問題解決了 – user2282596