[問題] C++中拷貝的問題
#include<iostream>
#include<vector>
#include<ctime>
using namespace std;
class Fraction{
public:
int p1;
int p2; //分數的class p1分子 p2分母
};
class Complex{
public:
int p1;
int p2; //定義複數 p1分子 p2分母
};
template<class T>
void NewElement(){
T x1;
srand(time(NULL));
x1.p1=rand()%99+1; //樣板 隨機產生複數或分數
x1.p2=rand()%99+1;
}
int main(){
vector<Fraction> fraction(8);
vector<Fraction>::iterator fraction_iterator;
vector<Complex> complex(8);
vector<Complex>::iterator complex_iterator;
for(fraction_iterator=fraction.begin();
fraction_iterator!=fraction.end(); fraction_iterator++){
NewElement<Fraction>();
*fraction_iterator=x1; //我的問題在這行 我想把NewElement中的分數
//拷貝到
} //fraction中 可是不能這樣做
//我該如何寫是好
for(complex_iterator=complex.begin(); complex_iterator!=complex.end();
complex_iterator++){
NewElement<Complex>(); //同上 問題在這行 我想把NewElement的複數拷貝到
*complex_iterator=x1; //complex中 可是不能這樣做
} //應該如何寫
system("PAUSE");
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 134.208.2.65
※ 編輯: ctyro 來自: 134.208.2.65 (05/09 18:09)
推
05/09 18:27, , 1F
05/09 18:27, 1F
→
05/09 18:34, , 2F
05/09 18:34, 2F
推
05/09 19:39, , 3F
05/09 19:39, 3F
→
05/09 19:42, , 4F
05/09 19:42, 4F
→
05/09 19:42, , 5F
05/09 19:42, 5F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章