Proč se mi nevolá funkce? – C / C++ – Fórum – Programujte.com
 x   TIP: Přetáhni ikonu na hlavní panel pro připnutí webu

Proč se mi nevolá funkce? – C / C++ – Fórum – Programujte.comProč se mi nevolá funkce? – C / C++ – Fórum – Programujte.com

 

Toto vlákno bylo označeno za vyřešené.
delicacyy0
Newbie
31. 8. 2012   #1
-
0
-

Zdravím,

postoupila jsem k dalším úkolům, co mám naprogramovat. Uvedu pozadí toho programu, pokud by jste chtěli být v obraze

Vytvořte program, který si od uživatele vyžádá souřadnice a poloměr pro dvě kružnice. Potom v jednom okně vykreslí tyto dvě kružnice a navíc třetí kružnici, jejíž střed bude ležet na spojnici středů těchto dvou kružnic a velikost poloměru bude průměrem z poloměru dvou zadaných kružnic. Použijte stejnou třídu Circle jako v předchozí úloze. Navíc v ní implementujte metody getCentreX(), getCentreY() a getRadius() pro získání hodnot příslušných členských proměnných. Dále mplementujte metodu setAverageCircle(), která přijme jako parametr dvě kružnice a z nich spočítá hodnoty svého středu a poloměru, jak je uvedeno výše. První kružnice bude vždy zelená, druhá modrá a třetí zprůměrovaná kružnice bude červená.

Jsem zatím u vykreslení spojnice metodou drawLine(), která zatím přijímá předem zadané parametry. Tato funkce se ale nevykonává. Proč? 

#include <iostream>
#include <g2.h>
#include <g2_X11.h>

using namespace std;
class Circle
{
  public:
    Circle();
    void setCentre(int ax, int ay);
    void setRadius(int r);
    void setColor(int c);
    void printValues() const;
    void draw();
    void drawLine();
    void readValues();
    void readCentre();
    void readRadius();
    void readColor();
  

  private:
    int x, y;
    int radius;
    int color;
};
Circle::Circle()
{
    x=1;
    y=1;
    radius=1;
    color=1;
}

void Circle::readCentre()
{
      int x, y;
      cout<<"Zadejte souradnice stredu: ";
      cin>>x>>y;
      this->setCentre(x,y);
}
void Circle::readRadius()
{
     int r;
     cout<<"Zadejte polomer: ";
     cin>>r;
     this->setRadius(r);
}

void Circle::readColor()
  {
      int c;
      cout<<"Zadejte barvu: ";
      cin>>c;
      setColor(c);
  }
void Circle::setCentre(int ax, int ay)
{
    x=ax;
    y=ay;
}

void Circle::setRadius(int r)
{
    radius=r;
}
void Circle::setColor(int c)
{
    color=c;
}

void Circle::printValues() const
{
    cout<<"Souradnice: "<<x<<", "<<y<<endl;
    cout<<"Polomer: "<<radius<<endl;
    cout<<"Barva: "<<color<<endl;
}
void Circle::draw()
{
    g2_set_line_width(0, 5);    // Tloustka cary bude 5 pixelu
    g2_pen(0, color);
    g2_circle(0, x, y, radius);
}

void Circle::drawLine()
{
    g2_set_line_width(2, 5);   
    g2_pen(2, 7);
    g2_line(2, 300, 400, 200, 250);
}
void Circle::readValues()
{
  readCentre();
  readRadius();
  readColor();
}

void drawCircles(Circle circ1, Circle circ2)
{
  circ1.draw();
  circ2.draw();
}
int dev = 0;
int main()
{
    Circle circ1, circ2;
   
    circ1.readValues();
    circ2.readValues();

    circ1.printValues();
    circ2.printValues();

    dev = g2_open_X11(600, 600);
    drawLine();
    drawCircles(circ1, circ2);
   
    cin.get();
    cin.get();
    return 0;
}
Nahlásit jako SPAM
IP: 78.45.255.–
KIIV
~ Moderátor
+43
God of flame
31. 8. 2012   #2
-
0
-

jestli ono to neni tim, ze je to metoda a ty to volas jako nejakou funkci, ktera ke tride nepatri

Nahlásit jako SPAM
IP: 93.91.152.–
Program vždy dělá to co naprogramujete, ne to co chcete...
delicacyy0
Newbie
31. 8. 2012   #3
-
0
-

#2 KIIV
jenže ona patří..nebo alespoň má patřit.. souřadnice kružnic budou využity jako souřadnice spojnice..

teď teda nevím, jestli nezaložit novou třídu.. ale to se mi zdá jako složitější řešení..

Nahlásit jako SPAM
IP: 78.45.255.–
liborb
~ Redaktor
+18
Guru
31. 8. 2012   #4
-
0
-

Z logiky věci vyplývá, že drawLine nemůže patřit třídě Circle neb ona musí pracovat s dvojicí souřadnic a když bude členská, tak bude mít jen jedny souřadnice. Udělej ji, stejně jako drawCircle nečlenskou a ty instance Circle jí předávej jako parametry. Tam pak využiješ ty metody getCentreX apod.

Nahlásit jako SPAM
IP: 78.80.52.–
KIIV
~ Moderátor
+43
God of flame
31. 8. 2012   #5
-
0
-

ale je receno ze se ma volat nad treti kruznici a ma ocekavat jako parametr dve dalsi kruznice

Nahlásit jako SPAM
IP: 93.91.152.–
Program vždy dělá to co naprogramujete, ne to co chcete...
delicacyy0
Newbie
31. 8. 2012   #6
-
0
-

#4 liborb
Áha, tak to pak jo. Zkusím to nějak zpatlat. Co jsem ale teď zkusila - udělat z drawLine() nečlenskou funkci, tak se stejně ta fuknce nevolá..

Nahlásit jako SPAM
IP: 78.45.255.–
31. 8. 2012   #7
-
0
-

Nevolá se nebo se nic nevykreslí? Zkus to odkrokovat v debuggeru.

hu

Nahlásit jako SPAM
IP: 195.178.67.–
delicacyy0
Newbie
31. 8. 2012   #8
-
0
-

#7 hlucheucho
nic nevykreslí,... přitom kód na tu čáru jsem úspěšně už používala jinde..

Nahlásit jako SPAM
IP: 78.45.255.–
KIIV
~ Moderátor
+43
God of flame
31. 8. 2012   #9
-
0
-

no hlavne ze to vypisuje hlasky jako No such device: 2

aa problem vyresen ... chtelo by to asi predavat i to "zarizeni"

Nahlásit jako SPAM
IP: 93.91.152.–
Program vždy dělá to co naprogramujete, ne to co chcete...
31. 8. 2012   #10
-
0
-

V jedné metodě používáš
 

g2_pen(0, color);

a v další
 

g2_pen(2, 7);

Jen věštím z křišťálové koule, nemám prototyp funkce.

hu

Nahlásit jako SPAM
IP: 195.178.67.–
delicacyy0
Newbie
31. 8. 2012   #11
-
0
-

#9 KIIV a #10 hlucheucho
to jsem poopravila už předtím, než jsem napsala příspěvek č.#7

Nahlásit jako SPAM
IP: 78.45.255.–
delicacyy0
Newbie
31. 8. 2012   #12
-
0
-

#10 hlucheucho
to g2_pen se může využívat vícekrát..

Nahlásit jako SPAM
IP: 78.45.255.–
KIIV
~ Moderátor
+43
God of flame
31. 8. 2012   #13
-
0
-

#12 delicacyy
jenze prvni parametr je "device" ... a ty mas jen jedno zarizeni (okno), melo by se pouzivat aspon to ...

Nahlásit jako SPAM
IP: 93.91.152.–
Program vždy dělá to co naprogramujete, ne to co chcete...
31. 8. 2012   #14
-
0
-

Navíc se stejným parametrem voláš i ostatní funkce g2_.... v metodě. Jestli je to "device", tak to nefunguje proto, že device 2 nemáš.

hu

Nahlásit jako SPAM
IP: 195.178.67.–
KIIV
~ Moderátor
+43
God of flame
31. 8. 2012   #15
-
0
-

#14 hlucheucho
coz je prekvapive ta hlaska, co sem tu hodil pred par odpovedi nazpet

Nahlásit jako SPAM
IP: 93.91.152.–
Program vždy dělá to co naprogramujete, ne to co chcete...
31. 8. 2012   #16
-
0
-

Pozdě jsem si všiml, že ti dělám ozvěnu :)

hu

Nahlásit jako SPAM
IP: 195.178.67.–
delicacyy0
Newbie
31. 8. 2012   #17
-
0
-

chlapci, vy vůbec nečtete, co jsem napsala...  už jsem to dávno opravila, ...tedy esi toto není dobře..

 já jsem to bez problémů užívala v c..

void drawLine()
{
    g2_set_line_width(dev, 5);    
    g2_pen(dev, 7);
    g2_line(dev, 430, 235, 430, 205);

}
Nahlásit jako SPAM
IP: 78.45.255.–
31. 8. 2012   #18
-
0
-

Je proměnná dev inicializovaná správně?

hu

Nahlásit jako SPAM
IP: 195.178.67.–
KIIV
~ Moderátor
+43
God of flame
31. 8. 2012   #19
-
0
-

no dev je definovany az pred main.. takze nic pred tim to nevidi... ale tezko rict jestli uz je to spravne.. novej kod tu neni

Nahlásit jako SPAM
IP: 93.91.152.–
Program vždy dělá to co naprogramujete, ne to co chcete...
delicacyy0
Newbie
31. 8. 2012   #20
-
0
-

#18 hlucheucho
v c jsem ji vždy inicializovala nulou a šlo to bez problému..

Nahlásit jako SPAM
IP: 78.45.255.–
delicacyy0
Newbie
31. 8. 2012   #21
-
0
-

#19 KIIV
to přeci nevadí, že je definováno před main jako globální proměnná ne? jako zkoušela jsem int dev = 0; švihnout dovnitř mainu i dovnitř drawLine(), ale je to furt stejné..

#include <iostream>
#include <g2.h>
#include <g2_X11.h>

using namespace std;

 class Circle
{
  public:
    Circle();
    void setCentre(int ax, int ay);
    void setRadius(int r);
    void setColor(int c);
    void printValues() const;
    void draw();
    
    void readValues();
    void readCentre();
    void readRadius();
    void readColor();
   

  private:
    int x, y;
    int radius;
    int color;
};

Circle::Circle()
{
    x=1;
    y=1;
    radius=1;
    color=1;
}

void Circle::readCentre()
 {
      int x, y;
      cout<<"Zadejte souradnice stredu: ";
      cin>>x>>y;
      this->setCentre(x,y);
 }

void Circle::readRadius()
{
     int r;
     cout<<"Zadejte polomer: ";
     cin>>r;
     this->setRadius(r);
 }

void Circle::readColor()
  {
      int c;
      cout<<"Zadejte barvu: ";
      cin>>c;
      setColor(c);
  }

void Circle::setCentre(int ax, int ay)
{
    x=ax;
    y=ay;
}

void Circle::setRadius(int r)
{
    radius=r;
}

void Circle::setColor(int c)
{
    color=c;
}

void Circle::printValues() const
{
    cout<<"Souradnice: "<<x<<", "<<y<<endl;
    cout<<"Polomer: "<<radius<<endl;
    cout<<"Barva: "<<color<<endl;
}

void Circle::draw()
{
    g2_set_line_width(dev, 5);    // Tloustka cary bude 5 pixelu
    g2_pen(dev, color);
    g2_circle(dev, x, y, radius);
}

void drawLine()
{
    g2_set_line_width(dev, 5);    
    g2_pen(dev, 7);
    g2_line(dev, 430, 235, 230, 205);

}

void Circle::readValues()
{
  readCentre();
  readRadius();
  readColor();
}

void drawCircles(Circle circ1, Circle circ2)
{
  circ1.draw();
  circ2.draw();
}

int dev = 0;
int main()
{
    Circle circ1, circ2;
    
    circ1.readValues();
    circ2.readValues();

    circ1.printValues();
    circ2.printValues();

    dev = g2_open_X11(600, 600);
    drawLine();
    drawCircles(circ1, circ2);
    
    cin.get();
    cin.get();
    return 0;
}
Nahlásit jako SPAM
IP: 78.45.255.–
31. 8. 2012   #22
-
0
-

KIIV myslel dát  int dev = 0;  před implementaci metod třídy.

hu

Nahlásit jako SPAM
IP: 195.178.67.–
delicacyy0
Newbie
31. 8. 2012   #23
-
0
-

hoši vy ze mě budete mít srandu, ale já celou dobu kompiluju circ1acirc2.cpp místo circ1acirc2_.cpp ... se vám velmi omlouvám..

Nahlásit jako SPAM
IP: 78.45.255.–
31. 8. 2012   #24
-
0
-

  

Nahlásit jako SPAM
IP: 195.178.67.–
delicacyy0
Newbie
31. 8. 2012   #25
-
0
-

#24 hlucheucho
ale to 'dát int dev = 0; před implementací metod třídy' by znamenalo dát to vyloženě dovnitř metod?

Nahlásit jako SPAM
IP: 78.45.255.–
KIIV
~ Moderátor
+43
God of flame
31. 8. 2012   #26
-
0
-

jenze se kompiluje odzhora dolu.. dat to za neco znamena ze to neco netusi, jakeho typu a co vubec pak dev je (pokud se pouzije pred deklaraci)

Nahlásit jako SPAM
IP: 93.91.152.–
Program vždy dělá to co naprogramujete, ne to co chcete...
delicacyy0
Newbie
31. 8. 2012   #27
-
0
-

#26 KIIV
aha.. já měla představu, že se postupuje nejdřív od main a pak se ty všechny objekty propojí

Nahlásit jako SPAM
IP: 78.45.255.–
31. 8. 2012   #28
-
+1
-
Zajímavé

#25 delicacyy
 

#include <iostream>
#include <g2.h>
#include <g2_X11.h>

using namespace std;

int dev = 0;

 class Circle
{
  public:
    Circle();
    void setCentre(int ax, int ay);
    void setRadius(int r);
    void setColor(int c);
    void printValues() const;
    void draw();
    
    void readValues();
    void readCentre();
    void readRadius();
    void readColor();
   

  private:
    int x, y;
    int radius;
    int color;
};

hu

Nahlásit jako SPAM
IP: 195.178.67.–
31. 8. 2012   #29
-
0
-

Pro hodnoty, které se nemění, se používají makra. Překladač při rozvoji makra dosadí jeho hodnotu ve všech výskytech makra
 

#define DEVICE	0	//definuji makro, jeho hodnota je 0

g2_set_line_width(DEVICE, 5);  //prekladac misto DEVICE dosadi 0
g2_pen(DEVICE, 7);

Pokud později (např. při ladění programu) potřebuješ hodnotu konstanty změnit, změníš ji pouze v definici makra a při dalším překladu ji překladač vymění všude, kde se vyskytuje.

hu

Nahlásit jako SPAM
IP: 195.178.67.–
KIIV
~ Moderátor
+43
God of flame
31. 8. 2012   #30
-
0
-

#29 hlucheucho
ale device ciste teoreticky nemusi dostat jen 0 ... o to cislo se stara  g2_open_X11() ..  z praktickeho hlediska by bylo lepsi mit device jako parametr pro draw a podobne... (a nebo pridat rovnou do tridy ale to neni tak idealni)

Nahlásit jako SPAM
IP: 93.91.152.–
Program vždy dělá to co naprogramujete, ne to co chcete...
delicacyy0
Newbie
31. 8. 2012   #31
-
0
-

#29 hlucheucho
díky, toto je velmi srozumitelné pro mě 

Nahlásit jako SPAM
IP: 78.45.255.–
31. 8. 2012   #32
-
0
-

#30 KIIV
Buď seš tak dobrej, nebo někde máš ty hlavičkový soubory s prototypy funkcí.  

Vědět, co je makro a jak se používá, se jí bude někdy hodit, ikdyž to asi není tento případ

hu

Nahlásit jako SPAM
IP: 195.178.67.–
KIIV
~ Moderátor
+43
God of flame
31. 8. 2012   #33
-
0
-

#32 hlucheucho
samozrejme sem tak dobrej.. + mam i ty hlavickovy soubory + mam to i prelozeny :D

+ dedukce mi napovida ze:  dev = g2_open_X11(600, 600);  snad ma i nejakou teoretickou funkci :D

Nahlásit jako SPAM
IP: 93.91.152.–
Program vždy dělá to co naprogramujete, ne to co chcete...
31. 8. 2012   #34
-
0
-

#33 KIIV
Máš pravdu. Jsem nejen hluchej, ale i slepej  

hu

Nahlásit jako SPAM
IP: 195.178.67.–
delicacyy0
Newbie
31. 8. 2012   #35
-
0
-

Tak jsem s tím programem dále pohla, potřebovala bych nyní poradit, jak doladit funkci setAverageCircle().. hlásí mi to chybu 'lvalue required as left operand of assignment' ..můžete mi objasnit, proč se ta hláška objevuje a jak jinak by se to dalo ošetřit?

#include <iostream>
#include <g2.h>
#include <g2_X11.h>

using namespace std;

 class Circle
{
  public:
    Circle();
    void setCentre(int ax, int ay);
    void setRadius(int r);
    void setColor(int c);
    void printValues() const;
    void draw();
    
    void readValues();
    void readCentre();
    void readRadius();
    void readColor();
    
    int getCentreX();
    int getCentreY();
    int getRadius();
    //void setAverageCircle();

  private:
    int x, y;
    int radius;
    int color;
};

Circle::Circle()
{
    x=1;
    y=1;
    radius=1;
    color=1;
}

void Circle::readCentre()
 {
      int x, y;
      cout<<"Zadejte souradnice stredu: ";
      cin>>x>>y;
      this->setCentre(x,y);
 }

void Circle::readRadius()
{
     int r;
     cout<<"Zadejte polomer: ";
     cin>>r;
     this->setRadius(r);
 }

void Circle::readColor()
  {
      int c;
      cout<<"Zadejte barvu: ";
      cin>>c;
      setColor(c);
  }

int Circle::getCentreX()
{
 return x;
}

int Circle::getCentreY()
{
 return y;
}

int Circle::getRadius()
{
 return radius;
}

void Circle::setCentre(int ax, int ay)
{
    x=ax;
    y=ay;
}

void Circle::setRadius(int r)
{
    radius=r;
}

void Circle::setColor(int c)
{
    color=c;
}

void Circle::printValues() const
{
    cout<<"Souradnice: "<<x<<", "<<y<<endl;
    cout<<"Polomer: "<<radius<<endl;
    cout<<"Barva: "<<color<<endl;
}

void printValuesCircles(Circle circ1, Circle circ2)
{
 cout << "Prvni kruznice:" << endl;
 cout << circ1.getCentreX() <<endl;
 cout << circ1.getCentreY() <<endl;
 cout << circ1.getRadius() <<endl;
 cout << "Druha kruznice:" <<endl;
 cout << circ2.getCentreX() <<endl;
 cout << circ2.getCentreY() <<endl;
 cout << circ2.getRadius() <<endl;
 
}

void setAverageCircle(Circle circ1, Circle circ2, Circle circ3)
{
 circ3.getCentreX() = (circ1.getCentreX() + circ2.getCentreX())/2;
 circ3.getCentreY() = (circ1.getCentreY() + circ2.getCentreY())/2;
 circ3.getRadius() = (circ1.getRadius() + circ2.getRadius())/2;
}

void Circle::draw()
{
    int dev = 0;
    g2_set_line_width(dev, 5);    // Tloustka cary bude 5 pixelu
    g2_pen(dev, color);
    g2_circle(dev, x, y, radius);
}

void drawLine(Circle circ1, Circle circ2)
{
    int dev = 0;
    g2_set_line_width(dev, 5);    
    g2_pen(dev, 7);
    g2_line(dev, circ1.getCentreX(), circ1.getCentreY(), circ2.getCentreX(), circ2.getCentreY());

}

void Circle::readValues()
{
  readCentre();
  readRadius();
  readColor();
}

void drawCircles(Circle circ1, Circle circ2, Circle circ3)
{

  circ1.draw();
  circ2.draw();
  circ3.draw();
}


int main()
{
    int dev = 0;
    //cout << "ahoj"<<endl;
    Circle circ1, circ2, circ3;
    
    circ1.readValues();
    circ2.readValues();

    circ1.printValues();
    circ2.printValues();
    
    printValuesCircles(circ1, circ2);

    dev = g2_open_X11(600, 600);
    drawLine(circ1, circ2);
    drawCircles(circ1, circ2, circ3);
    
    cin.get();
    cin.get();
    return 0;
}

void printValuesCircles() funkce je přidaná jenom proto, abych si ověřila, že fungují get_ fukce, takže si jí klidně nemusíte všímat, get_ funkce fungují

Nahlásit jako SPAM
IP: 78.45.255.–
31. 8. 2012   #36
-
0
-

circ3.getCentreX()   je metoda třídy, té nemůžeš přiřadit hodnotu

hu

Nahlásit jako SPAM
IP: 195.178.67.–
delicacyy0
Newbie
31. 8. 2012   #37
-
0
-

#36 hlucheucho
jenže když zadám místo circ3.getCentre() circ3.x, tak na mne překladač řve, že x je private... nebo se tady dají nějak využít případné ukazatele? (ale to je asi blbost, že..)

Nahlásit jako SPAM
IP: 78.45.255.–
31. 8. 2012   #38
-
0
-

Použij metody void setCentre(int ax, int ay); a  void setRadius(int r);, kde jako parametry použiješ výsledek výpočtu:
 

setRadius((circ1.getRadius() + circ2.getRadius())/2);

podobně i pro střed.

hu

Nahlásit jako SPAM
IP: 195.178.67.–
KIIV
~ Moderátor
+43
God of flame
31. 8. 2012   #39
-
0
-

co vyuzit setCentre?

Nahlásit jako SPAM
IP: 93.91.152.–
Program vždy dělá to co naprogramujete, ne to co chcete...
31. 8. 2012   #40
-
0
-

#39 KIIV
že by ozvěna?  

hu

Nahlásit jako SPAM
IP: 195.178.67.–
vitamin+8
Grafoman
31. 8. 2012   #41
-
0
-

#35 delicacyy
Len tak zo zvedavosti, vieš čo je R_value a L_value, aký je medzi nimi rozdiel a hlavne čo znamenala tá chyba?

Nahlásit jako SPAM
IP: 95.105.157.–
obfuscate: "The cruel god Malloc will strike you down. "
ZMeson: "That's the C god. C++ has a new god. "
delicacyy0
Newbie
31. 8. 2012   #42
-
0
-

#41 vitamin
nevím..R_value navíc slyším prva..

Nahlásit jako SPAM
IP: 78.45.255.–
31. 8. 2012   #43
-
0
-

S atributy private (soukromé) může manipulovat jen členská metoda třídy, z venku nejsou viditelné. Pokud jsou public (veřejné) je možné s nimi manipulovat i z venku třídy. Pokud by bylo: 

 class Circle
{
  public:
    Circle();
    void setCentre(int ax, int ay);
    void setRadius(int r);
    void setColor(int c);
    void printValues() const;
    void draw();
    
    void readValues();
    void readCentre();
    void readRadius();
    void readColor();
    
    int getCentreX();
    int getCentreY();
    int getRadius();
    //void setAverageCircle();

  //private:
    int x, y;
    int radius;
    int color;
};

pak můžeš udělat i:
 

circ3.radius = (circ1.getRadius() + circ2.getRadius())/2;

hu

Nahlásit jako SPAM
IP: 195.178.67.–
delicacyy0
Newbie
31. 8. 2012   #44
-
0
-

#43 hlucheucho
proč se tedy vlastně používá private? Aby do těch proměnných nešlo zasahovat?

Nahlásit jako SPAM
IP: 78.45.255.–
ingiraxo+15
Grafoman
31. 8. 2012   #45
-
0
-

#44 delicacyy
kvůli bezpečnosti... pokud nechceš, aby hodnotu šlo změnit odkudkoli i z instance, tak tomu dáš modifikátor "private" a půjde změnit pouze uvnitř třídy

Nahlásit jako SPAM
IP: 213.168.183.–
Moje aplikace: http://ophite.cz
Tutoriály na: C#
31. 8. 2012   #46
-
0
-

#45 ingiraxo
byls rychlejší

hu

Nahlásit jako SPAM
IP: 195.178.67.–
vitamin+8
Grafoman
31. 8. 2012   #47
-
+1
-
Zajímavé

#42 delicacyy
L(left) value je hodnota ktorá môže vystupovať na ľavej aj pravej strane výrazu.

R(right) value je hodnota ktorá môže vystupovať len na pravej strane výrazu.

Tá chyba ti vlastne povedala že si použila R value na ľavej strane výrazu.

R value sú konštanty a pomocné premenné a L value sú hlavne pomenované premenné, referencie a dereferencované pointre.

V tvojom prípade si zavolala metódu ktorá vracala int. Návratová hodnota sa zapísala do pomocnej premennej(r value) a potom si sa pokúšala do tejto premennej priradit inú hodnotu. Čiže pomocná premenná stála na ľavej strane výrazu a to nemôže. 

Nahlásit jako SPAM
IP: 95.105.157.–
obfuscate: "The cruel god Malloc will strike you down. "
ZMeson: "That's the C god. C++ has a new god. "
KIIV
~ Moderátor
+43
God of flame
31. 8. 2012   #48
-
+1
-
Zajímavé

v OOP existuje pojem "zapouzdreni" - tj. objekt ma presne dane rozhrani jak se s nim da manipulovat (neco jako zasilat zpravy co ma delat) a pak ma vnitrni promenne, do kterych nicemu okolo vesmes nic neni

ty kdyz pak dodrzis to rozhrani, muzes menit samotne streva objektu prakticky jak se ti zlibi a navenek se bude tvarit stale stejne...

Na druhou stranu, pokud budes pristupovat primo k promennym tak nejen ze tam muzes vlozit nesmysly, ale taky nesmis zmenit vnitrek objektu protoze uz se pouziva i jinde ... zmenit treba radius na r kdyz ho pouzivas asi tak 100x okolo jinde je "lahudka"

Nahlásit jako SPAM
IP: 93.91.152.–
Program vždy dělá to co naprogramujete, ne to co chcete...
KIIV
~ Moderátor
+43
God of flame
31. 8. 2012   #49
-
0
-

a k ty LVALUE - nic nebrani udelat metodu:    int & Radius() { return radius; }  ... to se pak da pouzit klidne i takto: 

obj.Radius() += 10;

ale je to zase vicemene trosku dira do zapouzdreni ... leda by radius vracel zase ten samy objekt a bylo nad nim implementovano += a tak   (ikdyz blbe by se poznavalo, k cemu se chce pricitat)

Nahlásit jako SPAM
IP: 93.91.152.–
Program vždy dělá to co naprogramujete, ne to co chcete...
delicacyy0
Newbie
31. 8. 2012   #50
-
0
-

tak to zatím vypadá takto:

#include <iostream>
#include <g2.h>
#include <g2_X11.h>

using namespace std;


 class Circle
{
  public:
    Circle();
    void setCentre(int ax, int ay);
    void setRadius(int r);
    void setColor(int c);
    void printValues() const;
    void draw();
    
    void readValues();
    void readCentre();
    void readRadius();
    void readColor();
    
    int getCentreX();
    int getCentreY();
    int getRadius();
    //void setAverageCircle();

  private:
    int x, y;
    int radius;
    int color;
};

Circle::Circle()
{
    x=1;
    y=1;
    radius=1;
    color=1;
}

void Circle::readCentre()
 {
      int x, y;
      cout<<"Zadejte souradnice stredu: ";
      cin>>x>>y;
      this->setCentre(x,y);
 }

void Circle::readRadius()
{
     int r;
     cout<<"Zadejte polomer: ";
     cin>>r;
     this->setRadius(r);
 }

void Circle::readColor()
  {
      int c;
      cout<<"Zadejte barvu: ";
      cin>>c;
      setColor(c);
  }

int Circle::getCentreX()
{
 return x;
}

int Circle::getCentreY()
{
 return y;
}

int Circle::getRadius()
{
 return radius;
}

void Circle::setCentre(int ax, int ay)
{
    x=ax;
    y=ay;
}

void Circle::setRadius(int r)
{
    radius=r;
}

void Circle::setColor(int c)
{
    color=c;
}

void Circle::printValues() const
{
    cout<<"Souradnice: "<<x<<", "<<y<<endl;
    cout<<"Polomer: "<<radius<<endl;
    cout<<"Barva: "<<color<<endl;
}

void printValuesCircles(Circle circ1, Circle circ2, Circle circ3)
{
 cout << "Prvni kruznice:" << endl;
 cout << circ1.getCentreX() <<endl;
 cout << circ1.getCentreY() <<endl;
 cout << circ1.getRadius() <<endl;
 cout << "Druha kruznice:" <<endl;
 cout << circ2.getCentreX() <<endl;
 cout << circ2.getCentreY() <<endl;
 cout << circ2.getRadius() <<endl;
 cout << "Treti kruznice:" <<endl;
 cout << circ3.getCentreX() <<endl;
 cout << circ3.getCentreY() <<endl;
 cout << circ3.getRadius() <<endl;
 
}

void setAverageCircle(Circle circ1, Circle circ2, Circle circ3)
{
 circ3.setCentre((circ1.getCentreX() + circ2.getCentreX())/2, (circ1.getCentreY() + circ2.getCentreY())/2);
 circ3.setRadius((circ1.getRadius() + circ2.getRadius())/2);
 circ3.setColor(25);
}

void Circle::draw()
{
    int dev = 0;
    g2_set_line_width(dev, 5);    // Tloustka cary bude 5 pixelu
    g2_pen(dev, color);
    g2_circle(dev, x, y, radius);
}

void drawLine(Circle circ1, Circle circ2)
{
    int dev = 0;
    g2_set_line_width(dev, 5);    
    g2_pen(dev, 7);
    g2_line(dev, circ1.getCentreX(), circ1.getCentreY(), circ2.getCentreX(), circ2.getCentreY());

}

void Circle::readValues()
{
  readCentre();
  readRadius();
  readColor();
}

void drawCircles(Circle circ1, Circle circ2, Circle circ3)
{

  circ1.draw();
  circ2.draw();
  circ3.draw();
}


int main()
{
    int dev = 0;
    cout << "ahoj"<<endl;
    Circle circ1, circ2, circ3;
    
    circ1.readValues();
    circ2.readValues();

    circ1.printValues();
    circ2.printValues();
    
    setAverageCircle(circ1, circ2, circ3);
    printValuesCircles(circ1, circ2, circ3);

    dev = g2_open_X11(600, 600);
    drawLine(circ1, circ2);
    drawCircles(circ1, circ2, circ3);
    
    cin.get();
    cin.get();
    return 0;
}

funkce printValuesCircles pro kontrolu, esi se správně načítají hodnoty však pro circ3 vrací jedničky  ..je to vidět i ve vykreslovacím okně, že se tam cosi malého v rohu krčí.. tak mě napadá, esi by nemomohlo místo int get_ funkcí zavést float get_funkce

Nahlásit jako SPAM
IP: 78.45.255.–
KIIV
~ Moderátor
+43
God of flame
31. 8. 2012   #51
-
0
-

ted je problem ze upravujes kopii circ3, ktera se udela tim volanim...

Nahlásit jako SPAM
IP: 93.91.152.–
Program vždy dělá to co naprogramujete, ne to co chcete...
delicacyy0
Newbie
31. 8. 2012   #52
-
0
-

#51 KIIV
kterým voláním?

Nahlásit jako SPAM
IP: 78.45.255.–
KIIV
~ Moderátor
+43
God of flame
31. 8. 2012   #53
-
0
-

setAverageCircle

Nahlásit jako SPAM
IP: 93.91.152.–
Program vždy dělá to co naprogramujete, ne to co chcete...
KIIV
~ Moderátor
+43
God of flame
31. 8. 2012   #54
-
0
-

totiz vsechno co das jen tak jako parametr, se predava hodnotou (udela se kopie a s tou se pracuje...) u takhle malyho objektu to jeste neni problem ale u neceho vetsiho by bylo lepsi pouzit napriklad:

setAverageCircle( Circle const & c1, Circle const & c2, Circle & c3 ) ...  (akorat by muselo byt u vsech tech getteru jeste klicove slovo  const  (jako ze nemeni vnitrek objektu) jinak to s toudle deklaraci nepobezi

Nahlásit jako SPAM
IP: 93.91.152.–
Program vždy dělá to co naprogramujete, ne to co chcete...
vitamin+8
Grafoman
31. 8. 2012   #55
-
0
-

Nech zakáže kopírovací konštruktor a uvidí(prekladač ukáže :) ) kde všade sa jej kopírujú objekty.

class Circle{
    public:
        //.....
        Circle(const Circle&) = delete;
};
Nahlásit jako SPAM
IP: 95.105.157.–
obfuscate: "The cruel god Malloc will strike you down. "
ZMeson: "That's the C god. C++ has a new god. "
reciproke0
Návštěvník
31. 8. 2012   #56
-
0
-

Ono pokavaď se upravuje jen ta jedna instance i pro přehlednost by bylo lepší upravit metodu setAverageCircle a volat ji nad tou instancí circ3. A pak v ní this.set....

circ3.setAverageCircle(Circle const & circ1, Circle const & circ2);

Nahlásit jako SPAM
IP: 82.208.4.–
delicacyy0
Newbie
3. 9. 2012   #57
-
0
-

Zdravím, zkoušela jsem to tedy překopat dneska na:

#include <iostream>
#include <g2.h>
#include <g2_X11.h>

using namespace std;

 class Circle
{
  public:
    Circle();
    //Circle(const Circle&) = delete;
    void setCentre(int ax, int ay);
    void setRadius(int r);
    void setColor(int c);
    void printValues() const;
    void draw();
    
    void readValues();
    void readCentre();
    void readRadius();
    void readColor();
    
    int getCentreX();
    int getCentreY();
    int getRadius();
    void setAverageCircle(Circle circ1, Circle circ2, Circle circ3);

  private:
    int x, y;
    int radius;
    int color;
};

Circle::Circle()
{
    x=1;
    y=1;
    radius=1;
    color=1;
}

void Circle::readCentre()
 {
      int x, y;
      cout<<"Zadejte souradnice stredu: ";
      cin>>x>>y;
      this->setCentre(x,y);
 }

void Circle::readRadius()
{
     int r;
     cout<<"Zadejte polomer: ";
     cin>>r;
     this->setRadius(r);
 }

void Circle::readColor()
  {
      int c;
      cout<<"Zadejte barvu: ";
      cin>>c;
      setColor(c);
  }

int Circle::getCentreX()
{
 return x;
}

int Circle::getCentreY()
{
 return y;
}

int Circle::getRadius()
{
 return radius;
}

void Circle::setCentre(int ax, int ay)
{
    x=ax;
    y=ay;
}

void Circle::setRadius(int r)
{
    radius=r;
}

void Circle::setColor(int c)
{
    color=c;
}

void Circle::printValues() const
{
    cout<<"Souradnice: "<<x<<", "<<y<<endl;
    cout<<"Polomer: "<<radius<<endl;
    cout<<"Barva: "<<color<<endl;
}

void printValuesCircles(Circle circ1, Circle circ2, Circle circ3)
{
 cout << "Prvni kruznice:" << endl;
 cout << circ1.getCentreX() <<endl;
 cout << circ1.getCentreY() <<endl;
 cout << circ1.getRadius() <<endl;
 cout << "Druha kruznice:" <<endl;
 cout << circ2.getCentreX() <<endl;
 cout << circ2.getCentreY() <<endl;
 cout << circ2.getRadius() <<endl;
 cout << "Treti kruznice:" <<endl;
 cout << circ3.getCentreX() <<endl;
 cout << circ3.getCentreY() <<endl;
 cout << circ3.getRadius() <<endl;
 
}

void Circle::setAverageCircle(Circle circ1, Circle circ2, Circle circ3)
{
 circ3.setCentre((circ1.getCentreX() + circ2.getCentreX())/2, (circ1.getCentreY() + circ2.getCentreY())/2);
 circ3.setRadius((circ1.getRadius() + circ2.getRadius())/2);
 circ3.setColor(25);
}

void Circle::draw()
{
    int dev = 0;
    g2_set_line_width(dev, 5);    // Tloustka cary bude 5 pixelu
    g2_pen(dev, color);
    g2_circle(dev, x, y, radius);
}

void drawLine(Circle circ1, Circle circ2)
{
    int dev = 0;
    g2_set_line_width(dev, 5);    
    g2_pen(dev, 7);
    g2_line(dev, circ1.getCentreX(), circ1.getCentreY(), circ2.getCentreX(), circ2.getCentreY());

}

void Circle::readValues()
{
  readCentre();
  readRadius();
  readColor();
}

void drawCircles(Circle circ1, Circle circ2, Circle circ3)
{

  circ1.draw();
  circ2.draw();
  circ3.draw();
}


int main()
{
    int dev = 0;
    cout << "ahoj"<<endl;
    Circle circ1, circ2, circ3;
    
    circ1.readValues();
    circ2.readValues();

    circ1.printValues();
    circ2.printValues();
    
    circ3.setAverageCircle(Circle const & circ1, Circle const & circ2);

    //setAverageCircle(circ1, circ2, circ3);
    printValuesCircles(circ1, circ2, circ3);

    dev = g2_open_X11(600, 600);
    drawLine(circ1, circ2);
    drawCircles(circ1, circ2, circ3);
    
    cin.get();
    cin.get();
    return 0;
}

jenže nevím si rady s const & protože nevím, co přesně to má znamenat a jak to tedy ošetřit.. poradíte prosím?

Nahlásit jako SPAM
IP: 78.45.255.–
liborb
~ Redaktor
+18
Guru
3. 9. 2012   #58
-
0
-

   

void Circle::setAverageCircle(Circle const & circ1, Circle const & circ2)
{
	setCentre((circ1.getCentreX() + circ2.getCentreX())/2, (circ1.getCentreY() + circ2.getCentreY())/2);
	setRadius((circ1.getRadius() + circ2.getRadius())/2);
	setColor(25);
}

...

circ3.setAverageCircle(circ1, circ2);
Nahlásit jako SPAM
IP: 78.80.52.–
delicacyy0
Newbie
3. 9. 2012   #59
-
0
-

#58 liborb
co ale napsat dovnitř do třídy?  void setAverageCircle(Circle circ1, Circle circ2); ani jiné obdoby, co jsem zkoušela i podle návrhu kompilátoru nejedou..

error: prototype for ‘void Circle::setAverageCircle(const Circle&, const Circle&)’ does not match any in class ‘Circle’
error: candidate is: void Circle::setAverageCircle(Circle, Circle)

Nahlásit jako SPAM
IP: 78.45.255.–
KIIV
~ Moderátor
+43
God of flame
3. 9. 2012   #60
-
0
-

#57 delicacyy
a pak uz jen  getCentreX, getCentreY a getRadius oznacit jako const (jako ze nemeni objekt) - stejne jako mas u printValues

Nahlásit jako SPAM
IP: 62.168.56.–
Program vždy dělá to co naprogramujete, ne to co chcete...
delicacyy0
Newbie
3. 9. 2012   #61
-
0
-

#60 KIIV
jenom pro ujištění: takže to 'const' se objeví ve třídě za getCentreX, getCentreY a getRadius a též (teď nevím, jak se tomu říká, ale objeví se tam, kde definujeme funkce v těchto formách ..void Circle::getCentreX const {..}).. do setAverageCircles() už ne?

Nahlásit jako SPAM
IP: 78.45.255.–
vitamin+8
Grafoman
3. 9. 2012   #62
-
0
-

 'const' sa nikde samo od seba neobjaví, musíš ho tam napísť. 

typedef int T;
class C{

        const T * const funkcia(const T x, T const y)const;
//        1         2             3            4       5

};

Tu máš deklaráciu metódy s 5 const.

1.  const znamená že pointer ukazuje na premennú ktorá sa nemôže meniť

2. const znamená že vrátený pointer sa nemôže meniť.

3. a 4. const znamenajú že parameter x a y sa nemôže meniť.

5. const znamená že metóda nemôže meniť žiadne atribúty triedy C (this môže byť const premenná). 

Nahlásit jako SPAM
IP: 95.105.157.–
obfuscate: "The cruel god Malloc will strike you down. "
ZMeson: "That's the C god. C++ has a new god. "
KIIV
~ Moderátor
+43
God of flame
3. 9. 2012   #63
-
0
-

#61 delicacyy
pomoci: int getCentreX() const; oznamujes prekladaci, ze se nic uvnitr objektu nebude menit touto metodou

metodu pak muzes pouzit i u konstantni instance tridy - coz se neda rici o metodach, ktere pomoci const oznaceny nejsou - muzou menit obsah a tudiz nemuzou byt volany nad konstantnim objektem

Nahlásit jako SPAM
IP: 62.168.56.–
Program vždy dělá to co naprogramujete, ne to co chcete...
delicacyy0
Newbie
3. 9. 2012   #64
-
0
-

už mi vše jede, jak má :)

jenom bych se chtěla zeptat na takovou drobnost, při kompilaci se mi totiž vypíše warning, že proměnná dev je sice nastavena, ale není užívána, avšak pokud ji odstraním, kompilátor ohlásí error že dev není deklarována... není mi jasné, proč mi to ten warning hlásí..

Nahlásit jako SPAM
IP: 78.45.255.–
KIIV
~ Moderátor
+43
God of flame
3. 9. 2012   #65
-
+1
-
Zajímavé

#64 delicacyy
int dev = 0;   se melo dat jen jednou pred samotnou tridu  (napriklad po using namespace std;)

a vynechat vsude jinde

Nahlásit jako SPAM
IP: 62.168.56.–
Program vždy dělá to co naprogramujete, ne to co chcete...
Zjistit počet nových příspěvků

Přidej příspěvek

Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!

Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku

×Vložení zdrojáku

×Vložení obrázku

Vložit URL obrázku Vybrat obrázek na disku
Vlož URL adresu obrázku:
Klikni a vyber obrázek z počítače:

×Vložení videa

Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×
 
Podporujeme Gravatara.
Zadej URL adresu Avatara (40 x 40 px) nebo emailovou adresu pro použití Gravatara.
Email nikam neukládáme, po získání Gravatara je zahozen.
-
Pravidla pro psaní příspěvků, používej diakritiku. ENTER pro nový odstavec, SHIFT + ENTER pro nový řádek.
Sledovat nové příspěvky (pouze pro přihlášené)
Sleduj vlákno a v případě přidání nového příspěvku o tom budeš vědět mezi prvními.
Reaguješ na příspěvek:

Uživatelé prohlížející si toto vlákno

Uživatelé on-line: 0 registrovaných, 32 hostů

Moderátoři diskuze

 

Hostujeme u Českého hostingu       ISSN 1801-1586       ⇡ Nahoru Webtea.cz logo © 20032024 Programujte.com
Zasadilo a pěstuje Webtea.cz, šéfredaktor Lukáš Churý