Implementacja sumy wektorów

0

Jako początkujący z C++ chciałbym prosić o pomoc. Takie zadanko:

Dodaj niezbedne metody i funkcje, aby możliwe stało się
wykonanie następujacego kodu

tutaj jest kod:

wektor w1(-1., -1., -1, "Globalny");
int main(){
wektor w1(1., -2., 1., "Lokalny");
cout << w1 << endl;
cout << ::w1 << endl;
cout << "Suma wektorow - " << w1 + ::w1 << endl;
if(w1 * ::w1 == 0) cout << "Wektory są prostopadłe";
else cout << "Wektory nie są prostopadłe";
return 0;
}

a to mam otrzymać jako dane wyjściowe:
screenshot-20231023191749.png

3

Co już masz napisane? Na forum się pomaga, a nie pisze za kogoś.

1

Poczytaj o czymś takim jak klasy i przeciążanie operatorów. Dasz radę.

0
Czitels napisał(a):

Poczytaj o czymś takim jak klasy i przeciążanie operatorów. Dasz radę.

Coś takiego jak tu

0
enedil napisał(a):

Co już masz napisane? Na forum się pomaga, a nie pisze za kogoś.

Głównie to to:


#include <iostream>
#include <string>
using namespace std;

class wektor{
    double x, y, z;
    public:
    wektor(double X = 0., double Y = 0., double Z = 0.){
    
    x = X;
    y = Y;
    z = Z;
    
    double getX(){return x;}
    double getY(){return y;}
    double getZ(){return z;}
    }
};

ostream& operator<<(ostream& strumien, wektor& w){
    strumien << "(" << w.getX() << ", " << w.getY() << ", " << w.getZ() << ")";
    return strumien;
}
wektor w1;

int main()
{
    wektor w1(1., -2., 1.);
    cout << w1 << endl;
    cout << ::w1 << endl;
    cout << "Suma wektorow - " << w1 + ::w1 << endl;
    if(w1 * ::w1 == 0) cout << "Wektory są prostopadłe";
    else cout << "Wektory nie są prostopadłe";
    
    return 0;
}
0
piotrek1998 napisał(a):
enedil napisał(a):

Co już masz napisane? Na forum się pomaga, a nie pisze za kogoś.

Głównie to to:


#include <iostream>
#include <string>
using namespace std;

class wektor{
    double x, y, z;
    public:
    wektor(double X = 0., double Y = 0., double Z = 0.){
    
    x = X;
    y = Y;
    z = Z;
    
    double getX(){return x;}
    double getY(){return y;}
    double getZ(){return z;}
    }
};

ostream& operator<<(ostream& strumien, wektor& w){
    strumien << "(" << w.getX() << ", " << w.getY() << ", " << w.getZ() << ")";
    return strumien;
}
wektor w1;

int main()
{
    wektor w1(1., -2., 1.);
    cout << w1 << endl;
    cout << ::w1 << endl;
    cout << "Suma wektorow - " << w1 + ::w1 << endl;
    if(w1 * ::w1 == 0) cout << "Wektory są prostopadłe";
    else cout << "Wektory nie są prostopadłe";
    
    return 0;
}

No i z czym masz problem? Postaraj się opisać go jak najjaśniej.

0

Formalnie dodałem tylko stringa do tego kodu bo wcześniej nie zawarłem:

#include <iostream>
#include <string>
using namespace std;

class wektor{
    double x, y, z;
    public:
    wektor(double X = 0., double Y = 0., double Z = 0., string = ""){
    
    x = X;
    y = Y;
    z = Z;
    
    double getX(){return x;}
    double getY(){return y;}
    double getZ(){return z;}
    string getNazwa();
    }
};

ostream& operator<<(ostream& strumien, wektor& w){
    strumien << "(" << w.getX() << ", " << w.getY() << ", " << w.getZ() << ", " << w.getNazwa() << ")";
    return strumien;
}
wektor w1(-1., -1., -1, "Globalny");

int main()
{
    wektor w1(1., -2., 1., "Lokalny");
    cout << w1 << endl;
    cout << ::w1 << endl;
    cout << "Suma wektorow - " << w1 + ::w1 << endl;
    if(w1 * ::w1 == 0) cout << "Wektory są prostopadłe";
    else cout << "Wektory nie są prostopadłe";
    
    return 0;
}

tylko pojawiają się takie błędy na wyjściu:

main.cpp: In constructor ‘wektor::wektor(double, double, double, std::string)’:
main.cpp:22:16: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
   22 |     double getX(){return x;}
      |                ^~
main.cpp:22:16: note: remove parentheses to default-initialize a variable
   22 |     double getX(){return x;}
      |                ^~
      |                --
main.cpp:22:16: note: or replace parentheses with braces to value-initialize a variable
main.cpp:22:18: error: a function-definition is not allowed here before ‘{’ token
   22 |     double getX(){return x;}
      |                  ^
main.cpp:23:16: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
   23 |     double getY(){return y;}
      |                ^~
main.cpp:23:16: note: remove parentheses to default-initialize a variable
   23 |     double getY(){return y;}
      |                ^~
      |                --
main.cpp:23:16: note: or replace parentheses with braces to value-initialize a variable
main.cpp:23:18: error: a function-definition is not allowed here before ‘{’ token
   23 |     double getY(){return y;}
      |                  ^
main.cpp:24:16: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
   24 |     double getZ(){return z;}
      |                ^~
main.cpp:24:16: note: remove parentheses to default-initialize a variable
   24 |     double getZ(){return z;}
      |                ^~
      |                --
main.cpp:24:16: note: or replace parentheses with braces to value-initialize a variable
main.cpp:24:18: error: a function-definition is not allowed here before ‘{’ token
   24 |     double getZ(){return z;}
      |                  ^
main.cpp:25:20: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
   25 |     string getNazwa();
      |                    ^~
main.cpp:25:20: note: remove parentheses to default-initialize a variable
   25 |     string getNazwa();
      |                    ^~
      |                    --
main.cpp: In function ‘std::ostream& operator<<(std::ostream&, wektor&)’:
main.cpp:30:26: error: ‘class wektor’ has no member named ‘getX’
   30 |     strumien << "(" << w.getX() << ", " << w.getY() << ", " << w.getZ() << ", " << w.getNazwa() << ")";
      |                          ^~~~
main.cpp:30:46: error: ‘class wektor’ has no member named ‘getY’
   30 |     strumien << "(" << w.getX() << ", " << w.getY() << ", " << w.getZ() << ", " << w.getNazwa() << ")";
      |                                              ^~~~
main.cpp:30:66: error: ‘class wektor’ has no member named ‘getZ’
   30 |     strumien << "(" << w.getX() << ", " << w.getY() << ", " << w.getZ() << ", " << w.getNazwa() << ")";
      |                                                                  ^~~~
main.cpp:30:86: error: ‘class wektor’ has no member named ‘getNazwa’
   30 |     strumien << "(" << w.getX() << ", " << w.getY() << ", " << w.getZ() << ", " << w.getNazwa() << ")";
      |                                                                                      ^~~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:40:38: error: no match for ‘operator+’ (operand types are ‘wektor’ and ‘wektor’)
   40 |     cout << "Suma wektorow - " << w1 + ::w1 << endl;
      |                                   ~~ ^ ~~~~
      |                                   |      |
      |                                   wektor wektor
In file included from /usr/include/c++/11/bits/stl_algobase.h:67,
                 from /usr/include/c++/11/bits/char_traits.h:39,
                 from /usr/include/c++/11/ios:40,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from main.cpp:9:
/usr/include/c++/11/bits/stl_iterator.h:568:5: note: candidate: ‘template std::reverse_iterator<_Iterator> std::operator+(typename std::reverse_iterator<_Iterator>::difference_type, const std::reverse_iterator<_Iterator>&)’
  568 |     operator+(typename reverse_iterator<_Iterator>::difference_type __n,
      |     ^~~~~~~~
/usr/include/c++/11/bits/stl_iterator.h:568:5: note:   template argument deduction/substitution failed:
main.cpp:40:42: note:   ‘wektor’ is not derived from ‘const std::reverse_iterator<_Iterator>’
   40 |     cout << "Suma wektorow - " << w1 + ::w1 << endl;
      |                                          ^~
In file included from /usr/include/c++/11/bits/stl_algobase.h:67,
                 from /usr/include/c++/11/bits/char_traits.h:39,
                 from /usr/include/c++/11/ios:40,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from main.cpp:9:
/usr/include/c++/11/bits/stl_iterator.h:1646:5: note: candidate: ‘template std::move_iterator<_IteratorL> std::operator+(typename std::move_iterator<_IteratorL>::difference_type, const std::move_iterator<_IteratorL>&)’
 1646 |     operator+(typename move_iterator<_Iterator>::difference_type __n,
      |     ^~~~~~~~
/usr/include/c++/11/bits/stl_iterator.h:1646:5: note:   template argument deduction/substitution failed:
main.cpp:40:42: note:   ‘wektor’ is not derived from ‘const std::move_iterator<_IteratorL>’
   40 |     cout << "Suma wektorow - " << w1 + ::w1 << endl;
      |                                          ^~
In file included from /usr/include/c++/11/string:55,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 from /usr/include/c++/11/ios:42,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from main.cpp:9:
/usr/include/c++/11/bits/basic_string.h:6095:5: note: candidate: ‘template std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)’
 6095 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/11/bits/basic_string.h:6095:5: note:   template argument deduction/substitution failed:
main.cpp:40:42: note:   ‘wektor’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
   40 |     cout << "Suma wektorow - " << w1 + ::w1 << endl;
      |                                          ^~
In file included from /usr/include/c++/11/string:56,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 from /usr/include/c++/11/ios:42,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from main.cpp:9:
/usr/include/c++/11/bits/basic_string.tcc:1169:5: note: candidate: ‘template std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const _CharT*, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)’
 1169 |     operator+(const _CharT* __lhs,
      |     ^~~~~~~~
/usr/include/c++/11/bits/basic_string.tcc:1169:5: note:   template argument deduction/substitution failed:
main.cpp:40:42: note:   mismatched types ‘const _CharT*’ and ‘wektor’
   40 |     cout << "Suma wektorow - " << w1 + ::w1 << endl;
      |                                          ^~
In file included from /usr/include/c++/11/string:56,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 from /usr/include/c++/11/ios:42,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from main.cpp:9:
/usr/include/c++/11/bits/basic_string.tcc:1189:5: note: candidate: ‘template std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(_CharT, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)’
 1189 |     operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
      |     ^~~~~~~~
/usr/include/c++/11/bits/basic_string.tcc:1189:5: note:   template argument deduction/substitution failed:
main.cpp:40:42: note:   ‘wektor’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
   40 |     cout << "Suma wektorow - " << w1 + ::w1 << endl;
      |                                          ^~
In file included from /usr/include/c++/11/string:55,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 from /usr/include/c++/11/ios:42,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from main.cpp:9:
/usr/include/c++/11/bits/basic_string.h:6132:5: note: candidate: ‘template std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)’
 6132 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/11/bits/basic_string.h:6132:5: note:   template argument deduction/substitution failed:
main.cpp:40:42: note:   ‘wektor’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
   40 |     cout << "Suma wektorow - " << w1 + ::w1 << endl;
      |                                          ^~
In file included from /usr/include/c++/11/string:55,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 from /usr/include/c++/11/ios:42,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from main.cpp:9:
/usr/include/c++/11/bits/basic_string.h:6148:5: note: candidate: ‘template std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, _CharT)’
 6148 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
      |     ^~~~~~~~
/usr/include/c++/11/bits/basic_string.h:6148:5: note:   template argument deduction/substitution failed:
main.cpp:40:42: note:   ‘wektor’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
   40 |     cout << "Suma wektorow - " << w1 + ::w1 << endl;
      |                                          ^~
In file included from /usr/include/c++/11/string:55,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 from /usr/include/c++/11/ios:42,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from main.cpp:9:
/usr/include/c++/11/bits/basic_string.h:6160:5: note: candidate: ‘template std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)’
 6160 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/include/c++/11/bits/basic_string.h:6160:5: note:   template argument deduction/substitution failed:
main.cpp:40:42: note:   ‘wektor’ is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
   40 |     cout << "Suma wektorow - " << w1 + ::w1 << endl;
      |                                          ^~
In file included from /usr/include/c++/11/string:55,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 from /usr/include/c++/11/ios:42,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from main.cpp:9:
/usr/include/c++/11/bits/basic_string.h:6166:5: note: candidate: ‘template std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&)’
 6166 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/11/bits/basic_string.h:6166:5: note:   template argument deduction/substitution failed:
main.cpp:40:42: note:   ‘wektor’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
   40 |     cout << "Suma wektorow - " << w1 + ::w1 << endl;
      |                                          ^~
In file included from /usr/include/c++/11/string:55,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 from /usr/include/c++/11/ios:42,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from main.cpp:9:
/usr/include/c++/11/bits/basic_string.h:6172:5: note: candidate: ‘template std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&)’
 6172 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/include/c++/11/bits/basic_string.h:6172:5: note:   template argument deduction/substitution failed:
main.cpp:40:42: note:   ‘wektor’ is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
   40 |     cout << "Suma wektorow - " << w1 + ::w1 << endl;
      |                                          ^~
In file included from /usr/include/c++/11/string:55,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 from /usr/include/c++/11/ios:42,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from main.cpp:9:
/usr/include/c++/11/bits/basic_string.h:6194:5: note: candidate: ‘template std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const _CharT*, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&)’
 6194 |     operator+(const _CharT* __lhs,
      |     ^~~~~~~~
/usr/include/c++/11/bits/basic_string.h:6194:5: note:   template argument deduction/substitution failed:
main.cpp:40:42: note:   mismatched types ‘const _CharT*’ and ‘wektor’
   40 |     cout << "Suma wektorow - " << w1 + ::w1 << endl;
      |                                          ^~
In file included from /usr/include/c++/11/string:55,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 from /usr/include/c++/11/ios:42,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from main.cpp:9:
/usr/include/c++/11/bits/basic_string.h:6200:5: note: candidate: ‘template std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(_CharT, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&)’
 6200 |     operator+(_CharT __lhs,
      |     ^~~~~~~~
/usr/include/c++/11/bits/basic_string.h:6200:5: note:   template argument deduction/substitution failed:
main.cpp:40:42: note:   ‘wektor’ is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
   40 |     cout << "Suma wektorow - " << w1 + ::w1 << endl;
      |                                          ^~
In file included from /usr/include/c++/11/string:55,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 from /usr/include/c++/11/ios:42,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from main.cpp:9:
/usr/include/c++/11/bits/basic_string.h:6206:5: note: candidate: ‘template std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&, const _CharT*)’
 6206 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/include/c++/11/bits/basic_string.h:6206:5: note:   template argument deduction/substitution failed:
main.cpp:40:42: note:   ‘wektor’ is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
   40 |     cout << "Suma wektorow - " << w1 + ::w1 << endl;
      |                                          ^~
In file included from /usr/include/c++/11/string:55,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 from /usr/include/c++/11/ios:42,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from main.cpp:9:
/usr/include/c++/11/bits/basic_string.h:6212:5: note: candidate: ‘template std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&, _CharT)’
 6212 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/include/c++/11/bits/basic_string.h:6212:5: note:   template argument deduction/substitution failed:
main.cpp:40:42: note:   ‘wektor’ is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
   40 |     cout << "Suma wektorow - " << w1 + ::w1 << endl;
      |                                          ^~
main.cpp:41:11: error: no match for ‘operator*’ (operand types are ‘wektor’ and ‘wektor’)
   41 |     if(w1 * ::w1 == 0) cout << "Wektory są prostopadłe";
      |        ~~ ^ ~~~~
      |        |      |
      |        wektor wektor
2

Podpowiem ci co jest źle. Spójrz tylko na swój kod, po tym jak już został poprawnie sformatowany:

#include <iostream>

#include <string>

using namespace std;

class wektor {
  double x, y, z;
  public:
    wektor(double X = 0., double Y = 0., double Z = 0., string = "") {

      x = X;
      y = Y;
      z = Z;

      double getX() {
        return x;
      }
      double getY() {
        return y;
      }
      double getZ() {
        return z;
      }
      string getNazwa();
    }
};

ostream & operator << (ostream & strumien, wektor & w) {
  strumien << "(" << w.getX() << ", " << w.getY() << ", " << w.getZ() << ", " << w.getNazwa() << ")";
  return strumien;
}
wektor w1(-1., -1., -1, "Globalny");

int main() {
  wektor w1(1., -2., 1., "Lokalny");
  cout << w1 << endl;
  cout << ::w1 << endl;
  cout << "Suma wektorow - " << w1 + ::w1 << endl;
  if (w1 * ::w1 == 0) cout << "Wektory są prostopadłe";
  else cout << "Wektory nie są prostopadłe";

  return 0;
}

Widzisz co jest źle?

0

Co robie w konstruktorze wektora?

 wektor(double X = 0., double Y = 0., double Z = 0., string = "")

definiuje domyślne parametry konstruktora.
Potem przypisuje wartości do zmiennych zdefiniowanych w klasie wektor:

    x = X;
    y = Y;
    z = Z;

a następnie wywołuje wartości zwracane:

    double getX(){return x;}
    double getY(){return y;}
    double getZ(){return z;}
    string getNazwa(){}
4

Metody getX, itd., nie mogą być częścią konstruktora, tylko kolejnymi metodami klasy.

Jeśli w ogóle mają istnieć, bo samemu mam bardzo negatywną opinię na temat „gołych” getterów i setterów, ale to akurat kwestia stylu; podczas gdy umieszczanie ich w innej funkcji jest kwestią składni — więc jest obiektywnie niepoprawne.

0
Althorion napisał(a):

Metody getX, itd., nie mogą być częścią konstruktora, tylko kolejnymi metodami klasy.

Jeśli w ogóle mają istnieć, bo samemu mam bardzo negatywną opinię na temat „gołych” getterów i setterów, ale to akurat kwestia stylu; podczas gdy umieszczanie ich w innej funkcji jest kwestią składni — więc jest obiektywnie niepoprawne.

Tak?

class wektor{
    double x, y, z;
    public:
    wektor(double X = 0., double Y = 0., double Z = 0., string = ""){
    
    x = X;
    y = Y;
    z = Z;

    }
    double getX(){return x;}
    double getY(){return y;}
    double getZ(){return z;}
    string getNazwa(){}
};
3

Już prawie dobrze — Twoja funkcja getNazwa obiecała, że będzie zwracać stringa (bo w końcu jest string getNazwa()), a tego nie robi — nie zwraca nic.

0
Althorion napisał(a):

Już prawie dobrze — Twoja funkcja getNazwa obiecała, że będzie zwracać stringa (bo w końcu jest string getNazwa()), a tego nie robi — nie zwraca nic.

class wektor{
    double x, y, z;
    public:
    wektor(double X = 0., double Y = 0., double Z = 0., string = ""){
    
    x = X;
    y = Y;
    z = Z;

    }
    double getX(){return x;}
    double getY(){return y;}
    double getZ(){return z;}
    string getNazwa(){return string;}
};
1
 wektor(double X = 0., double Y = 0., double Z = 0., string = "")

Czy string = "" jest poprawnym zapisem? To wygląda dla mnie jak błąd składni, ale dawno nie pisałem w C++. Ale dla mnie to wygląda tak jakby brakowało tu nazwy zmiennej.

0

Co jeszcze ?

2

No co jeszcze? Odpowiednie przeciążenia operatorów (+, *, <<)

0

@enedil: w sensie masz to na myśli?

ostream& operator<<(ostream& strumien, wektor& w){
    strumien << "(" << w.getX() << ", " << w.getY() << ", " << w.getZ() << ", " << w.getNazwa() << ")";
    return strumien;
}
1
piotrek1998 napisał(a):

@enedil: w sensie masz to na myśli?

ostream& operator<<(ostream& strumien, wektor& w){
    strumien << "(" << w.getX() << ", " << w.getY() << ", " << w.getZ() << ", " << w.getNazwa() << ")";
    return strumien;
}

No nie do końca. Przeczytaj jeszcze raz czego się od ciebie oczekuje na wyjściu.

0

wektor globalny wraz z argumentami, to samo wektor lokalny, suma wektorów i komunikat, że wektor jest albo nie prostopadły. No napisałeś odpowiednie przeciążenia operatorów, więc co innego?

1
piotrek1998 napisał(a):

wektor jest albo nie prostopadły.

Czyli będziesz potrzebował zaimplementować tzw. dot product (iloczyn skalarny), czyli sumę iloczynów poszczególnych współrzędnych
https://www.matemaks.pl/iloczyn-skalarny-wektorow.html
https://pl.wikipedia.org/wiki/Iloczyn_skalarny
jeśli wynosi zero, to wektory są prostopadłe

wg polecenia każą ci to zaimplementować jako operator *:

if(w1 * ::w1 == 0) cout << "Wektory są prostopadłe";
1

No i o tym mówię. Musisz umieć wypisać, że wektor jest lokalny, albo globalny. Jak to zrobisz? Poza tym, czemu wybrałeś typ double?

0

No właśnie jak? Myślałem że jest to już zrobione.w int main

2

W dostarczonym Ci kodzie, linijki które wypisują coś na ekran to:

cout << w1 << endl;  // Wektor Lokalny: (1, -2, 1)
cout << ::w1 << endl;  // Wektor Globalny: (-1, -1, -1)
cout << "Suma wektorow - " << w1 + ::w1 << endl;  // Suma wektorów - Wektor : (0, -3, 0)
if(w1 * ::w1 == 0) cout << "Wektory są prostopadłe";
else cout << "Wektory nie są prostopadłe";  // Wektory nie są prostopadłe

(jak mnie to formatowanie drażni, to ludzka mowa nie opisze… 🙄)

Żeby to osiągnąć, musisz odpowiednio przeciążyć operator<<, żeby „się dogadał” z coutem. Masz już dobry start tutaj, ale forma, którą wypisujesz, jest inna, niż chce zadanie — przyjrzyj się zwłaszcza pierwszym dwóm linijkom wyżej, tam najłatwiej zobaczyć, jaka jest oczekiwana postać.

4

https://godbolt.org/z/dKWjT9rMc

#include <format>
#include <iostream>
#include <string>
#include <utility>

class wektor {
    double x, y, z;
    std::string name;

public:
    explicit wektor(double X = 0., double Y = 0., double Z = 0., std::string name = "")
        : x { X }
        , y { Y }
        , z { Z }
        , name { std::move(name) }
    {
    }

    [[nodiscard]] double getX() const { return x; }
    [[nodiscard]] double getY() const { return y; }
    [[nodiscard]] double getZ() const { return z; }

    [[nodiscard]] wektor sum(const wektor& b) const
    {
        return wektor { x + b.x, y + b.y, z + b.z, std::format("{}+{}", name, b.name) };
    }

    [[nodiscard]] double dotProduct(const wektor& b) const
    {
        return x * b.x + y * b.y + z * b.z;
    }
    [[nodiscard]] std::string getNazwa() const { return name; }
};

std::ostream& operator<<(std::ostream& strumien, const wektor& w)
{
    strumien << "(" << w.getX() << ", " << w.getY() << ", " << w.getZ() << ", " << w.getNazwa() << ")";
    return strumien;
}

wektor operator+(const wektor& a, const wektor& b)
{
    return a.sum(b);
}

double operator*(const wektor& a, const wektor& b)
{
    return a.dotProduct(b);
}

const wektor w1(-1., -1., -1, "Globalny");

int main()
{
    const wektor w1(1., -2., 1., "Lokalny");
    std::cout << w1 << '\n';
    std::cout << ::w1 << '\n';
    std::cout << "Suma wektorow - " << (w1 + ::w1) << '\n';
    if (w1 * ::w1 == 0) {
        std::cout << "Wektory są prostopadłe";
    } else {
        std::cout << "Wektory nie są prostopadłe";
    }
    return 0;
}

Zostawiłem nazewnictwo, mimo, że jest niekonsekwentne, cześć jest po polsku, cześć po angielsku.
Kod powinno się pisać tylko po angielsku, bo wszystkie biblioteki mają symbole angielskie, więc tylko angielskim uzyskuje się konsystencję.
Nie mówiąc już o tym, że pisanie kodu w języku ojczystym, dzieli przyszłe zarobki przez 2 lub 3.

0

Co do tego przeciążania operatorów, o którym wspominaliście wyżej, choć pewności nie mam jako początkujący to chodziło wam o takie coś:

wektor operator + (const wektor & lhs, const wektor & rhs) {
    
    return wektor(lhs.getX()+rhs.getX(),
                  lhs.getY()+rhs.getY(),
                  lhs.getZ()+rhs.getZ(),
                  "Suma "+lhs.getNazwa()+" i "+rhs.getNazwa());
}
double operator * (const wektor & lhs,const wektor & rhs) {
    double iloczyn = lhs.getX()*rhs.getX()+lhs.getY()*rhs.getY()+lhs.getZ()*rhs.getZ();
    return std::fabs(iloczyn)<1e-14?0:iloczyn;
0

Słuchajcie, potrzebne mi to na jutro. Dacie rade. Nie wiem ale chyba zostało już tylko wypisanie tych komunikatów?

0

No to pytam. Bo pisaliście o przeciążeniu operatorów. Więc czy to jest to o co chodzi?

wektor operator +
double operator *

jakbym wiedział to bym nie pytał. Serio.

2

operator overloading po angielsku, weź sobie poszukaj.

Zadajesz pytania na tematy, które faktycznie mogą być trudne (działania na wektorach, elementy języka tobie nieznane itp.). Ale trudność tematu to jedno, a brak inicjatywy i roszczeniowe podejście Słuchajcie, potrzebne mi to na jutro. Dacie rade. to zupełnie inna sprawa.

0
LukeJL napisał(a):

Ale trudność tematu to jedno, a brak inicjatywy i roszczeniowe podejście Słuchajcie, potrzebne mi to na jutro. Dacie rade. to zupełnie inna sprawa.

Rozumiem, ale nie ode mnie to zależy:
screenshot-20231024224103.png

0

Myślałem, żeby coś takiego napisać

wektor operator + (const wektor & lhs, const wektor & rhs) {
    
    return wektor(lhs.getX()+rhs.getX(),
                  lhs.getY()+rhs.getY(),
                  lhs.getZ()+rhs.getZ(),
                  "Suma "+lhs.getNazwa()+" i "+rhs.getNazwa());
}
double operator * (const wektor & lhs,const wektor & rhs) {
    double iloczyn = lhs.getX()*rhs.getX()+lhs.getY()*rhs.getY()+lhs.getZ()*rhs.getZ();
    return std::fabs(iloczyn)<1e-14?0:iloczyn;
}

ale trzeba wprowadzić jakieś zmiany. Co bym musiał tu zmienić, żeby to zadziałało?

1 użytkowników online, w tym zalogowanych: 0, gości: 1