質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

2回答

3604閲覧

C++ error: passing 'A' as 'this' argument of 'B' discards qualifiers [-fpermissive] について

amber_snob

総合スコア30

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2019/07/28 18:55

編集2019/07/28 18:57

c++

1#include "Complex.h" 2 3int main(){ 4 const Complex c0(4.0, 5.5); 5 const Complex c1(1.1, 2.2); 6 Complex result0 = c0+c1; 7 Complex result1 = c0*c1; 8 result0.print(); 9 result1.print(); 10}

operatorを使った複素数の加算・乗算のソースコードです
これを実行したところ、

error: passing 'const Complex' as 'this' argument of 'Complex Complex::operator+(const Complex&)' discards qualifiers [-fpermissive]

といったエラーが出ました。
原因が分からないので詳しい方がいらっしゃいましたら教えていただきたいです。

"Complex.h"の中身は以下のようになります。

c++

1#ifndef COMPLEX_H_ 2#define COMPLEX_H_ 3 4#include <cmath> 5#include <climits> 6#include <string> 7#include <iostream> 8#include <stdio.h> 9#include <sstream> 10 11class Complex { 12private: 13 double real; // 実部 14 double imag; // 虚部 15public: 16 /* 17 Complex(double re, double im); // 引数付コンストラクタ 18 Complex(){real = 0; imag = 0;} //デフォルトコンストラクタ 19 Complex(const Complex& orig); // コピーコンストラクタ 20 Complex(double re){real = re; imag = 0;} //変換コンストラクタ 21 22 //後置増分演算子 23 Complex operator+() { return *this; } // 単項演算子+c 24 Complex operator-() { return Complex(-real, -imag); } // 単項演算子-c 25 Complex operator+(const Complex& arg); // 加算 26 // Complex operator-(const Complex& arg); // 減算は不要 27 Complex operator*(const Complex& arg); // 乗算 28 29 // 前置増分演算子 30 Complex& operator+=(const Complex& arg); // 複合演算子 31 Complex& operator-=(const Complex& arg); // 複合演算子 32 Complex& operator*=(const Complex& arg); // 複合演算子 33 34 bool operator==(const Complex& arg); // 等号 35 bool operator!=(const Complex& arg); // 不等号 36 37 double modulus(); // 絶対値 38 std::string print(); // 3+4i のような表示文字列 39 */ 40 41 Complex(double re, double im){ // 引数付コンストラクタ 42 real = re; imag = im; 43 } 44 45 Complex(){real = 0; imag = 0;} //デフォルトコンストラクタ 46 47 Complex(const Complex& orig){ // コピーコンストラクタ 48 real = orig.real; imag = orig.imag; 49 } 50 51 Complex(double re){real = re; imag = 0;} //変換コンストラクタ 52 53 // 後置増分演算子 54 Complex operator+() { return *this; } // 単項演算子+c 55 56 Complex operator-() { return Complex(-real, -imag); } // 単項演算子-c 57 58 Complex operator+(const Complex& arg){ // 加算 59 return arg; 60 } 61 62 Complex operator*(const Complex& arg){ // 乗算 63 return Complex(-arg.real, -arg.imag); 64 } 65 66 // 前置増分演算子 67 Complex& operator+=(const Complex& arg){ 68 if(real < UINT_MAX) real += arg.real; 69 return *this; 70 } 71 72 Complex& operator-=(const Complex& arg){ 73 if(real < UINT_MAX) real -= arg.real; 74 return *this; 75 } 76 77 Complex& operator*=(const Complex& arg){ 78 if(real < UINT_MAX) real *= arg.real; 79 return *this; 80 } 81 82 bool operator==(const Complex& arg){ 83 return *this == arg; 84 } 85 86 bool operator!=(const Complex& arg){ 87 return *this != arg; 88 } 89 90 double modulus(){ 91 return sqrt(real*real + imag*imag); 92 } 93 94 std::string print(){ 95 std::ostringstream oss; 96 oss << real << "+" << imag << "i"; 97 printf("%s\n",oss.str().c_str()); 98 return 0; 99 //std::string r = std::to_string(real); 100 //std::string i = std::to_string(imag); 101 //return r + "+" + i + "i"; 102 } 103}; 104 105#endif /* COMPLEX_H_ */ 106

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

constオブジェクトを介しては、const修飾されたメンバ関数しか呼べません。
これは知らない内にメンバが書き換えられるのを避ける為です。

そして、const修飾するべき箇所はエラーメッセージに記載されているとおりです。
二項加算演算を担うメンバ関数ですね。

error: passing 'const Complex' as 'this' argument of 'Complex Complex::operator+(const Complex&)' discards qualifiers [-fpermissive]

その他変更が生じ得ないメンバ関数も、const修飾した方が分かり良いでしょう。


両オペランドに影響を与えないのであれば、
非メンバ関数でオーバーロードを実現するのが素直であるように思います。

C++

1Complex operator+(const Complex& left, const Complex& right) { 2 ... 3}

フレンド関数にしてやれば、Complexクラスのprivateメンバにもアクセス可能です。

投稿2019/07/28 22:56

LouiS0616

総合スコア35660

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

amber_snob

2019/07/29 01:54

ありがとうございます!無事解決しました
guest

0

メンバ関数 print() が const じゃないからじゃない?

投稿2019/07/28 19:36

episteme

総合スコア16614

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問