[英]Private arguments not saving things in C++ program
主程序
#include <iostream>
#include <string>
#include <vector>
#include "Figuras.h"
using namespace std;
void error(){
cout<<"algo ha ido mal vuelve a empezar"<<endl;
exit(0);
}
int main(){
Figuras fig;
vec V;
string tip;
cout << "Triangle = Tr / Cuadrat = Cu / Rectangle = Re / Segment = Se / Cercle = Ce" << endl;
cin >> tip;
if(tip=="Tr"){
V = vector<pair<double,double>>(3);
}
else if(tip == "Cu" or tip == "Re"){
V = vector<pair<double,double>>(4);
}
else if (tip == "Se" or tip == "Ce"){
V = vector<pair<double,double>>(2);
}
else error();
for (int i = 0; i < V.size(); i++) { //fill vector with coords
cout << "Introdueix coordenada numero: " << i<<" format (double,double): " <<endl;
cout << "x: ";
cin >> V[i].first;
cout << "y: ";
cin >> V[i].second;
}
fig.crea_figura(tip,V);
fig.mostrar_fig();
}
圖形.cpp
#include "Figuras.h"
using namespace std;
/*void Figuras::Figura(){
tipo = NULL;
Coord = NULL;
}*/
string Figuras::get_tipo (){
return tipo;
};
vec Figuras::get_coords (){
return Coord;
};
punto Figuras::get_inicio (){
return inicio;
};
void Figuras::Set_tipo (string typ){
tipo = typ;
};
void Figuras::Set_Coord (vec V){
punto p;
int x= V.size();
Coord=vector<pair<double,double>>(x);
for ( int i =0; i<x; i++){
Coord[i].first = p.first;
Coord[i].second = p.second;
}
};
void Figuras::Set_inicio (punto ini){
inicio = ini;
};
void Figuras::crea_figura (string tip, vec V){
Set_tipo(tip);
Set_Coord(V);
};
void Figuras::trasladar (punto P){
double difx,dify;
difx=P.first - inicio.first;
dify=P.second - inicio.second;
vec V;
Figuras::get_coords();
if (!Coord.empty()){
for(int i=0; i<Coord.size(); i++){
Coord[i].first += difx;
Coord[i].second += dify;
}
}
else cout<< "no hay coords"<<endl;
};
void Figuras::mostrar_fig (){
vec V;
V=get_coords();
punto ini;
ini=get_inicio();
string tip;
tip=get_tipo();
cout<<"tipo: "<<tip<<endl;
for (int i =0; i<V.size(); i++){
cout<<"punto "<< i+1<<": "<<"("<<V[i].first<<","<<V[i].second<<")"<<endl;
}
cout<< "punto inicial: ("<<ini.first<<","<<ini.second<<")"<<endl;
};
Figuras.h
#ifndef FIGURAS_H
#define FIGURAS_H
#include <vector>
#include <iostream>
#include <string>
using namespace std;
typedef vector < pair < double, double >>vec;
typedef pair < double, double >punto;
class Figuras
{
private:
string tipo;
vec Coord;
punto inicio={0.0,0.0};
public:
string get_tipo ();
vec get_coords ();
punto get_inicio ();
void Set_tipo (string typ);
void Set_Coord (vec V);
void Set_inicio (punto ini);
void crea_figura (string tip, vec vec);
void trasladar (punto P);
void mostrar_fig ();
};
#endif
當我做的時候,讓我解釋一下這個問題, crea_figura(tip,V);
然后在 main.cpp 第 39,40 行上的 mostrar_fig()。
有時候是這樣的:
Triangle = Tr / Cuadrat = Cu / Rectangle = Re / Segment = Se / Cercle = Ce
Tr
Introdueix coordenada numero: 0 format (double,double):
x: 1.1
y: 1.1
Introdueix coordenada numero: 1 format (double,double):
x: 2.2
y: 2.2
Introdueix coordenada numero: 2 format (double,double):
x: 3.1
y: 1.1
tipo: Tr
punto 1: (0,0)
punto 2: (0,0)
punto 3: (0,0)
punto inicial: (0,0)
所有的點似乎都是空的,我不明白為什么會這樣。 有沒有其他方法來初始化類(另一種類型的構造函數)?
看看這個代碼
void Figuras::Set_Coord (vec V){
punto p;
int x= V.size();
Coord=vector<pair<double,double>>(x);
for ( int i =0; i<x; i++){
Coord[i].first = p.first;
Coord[i].second = p.second;
}
};
p
是一個空點,這就是為什么你所有的點都是空的。
你正在以困難的方式做事,真的應該很容易。 嘗試這個
void Figuras::Set_Coord (vec V){
Coord = V;
}
這就是你需要做的。
上面的代碼是正確的,但為了更有效的方式,你應該通過引用傳遞。
void Figuras::Set_Coord (const vec& V){
Coord = v;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.