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

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

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

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

Q&A

1回答

1081閲覧

仮想関数、抽象関数をもちいたシミュレーション

tadayuki

総合スコア0

C++

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

0グッド

0クリップ

投稿2021/07/06 02:33

ここに質問の内容を詳しく書いてください。
輻輳制御アルゴリズムのシミュレーターを作成し、cwndの推移を確認するプログラムです。
ちなみにcwndの初期値は1、ssThreshの初期値は250です。
アルゴリズムの動作をクラスを用いて派生しました。
今回わからない点は、simulate関数の引数のalgorithmです。
クラスを引数とする場合にはどのようにプログラムを構築すればよいですか。

エラーメッセージ
fukusou.cpp: In function ‘void simulate(std::__cxx11::string*, const Base&)’:
fukusou.cpp:53:25: error: passing ‘const Base’ as ‘this’ argument discards qualifiers [-fpermissive]
algorithm.Timeout();
^
fukusou.cpp:11:8: note: in call to ‘void Base::Timeout()’
void Timeout(){
^~~~~~~
fukusou.cpp:59:31: error: passing ‘const Base’ as ‘this’ argument discards qualifiers [-fpermissive]
algorithm.increase_cwnd();
^
fukusou.cpp:14:8: note: in call to ‘void Base::increase_cwnd()’
void increase_cwnd(){

fukusou.cpp: In function ‘int main()’:
fukusou.cpp:72:9: error: cannot declare variable ‘algo_1’ to be of abstract type ‘Tahoe’
Tahoe algo_1(1,250);

fukusou.cpp:25:7: note: because the following virtual functions are pure within ‘Tahoe’:
class Tahoe:public Base{

fukusou.cpp:22:16: note: virtual void Base::NEW_ssThresh() const
virtual void NEW_ssThresh() const=0;

fukusou.cpp:73:8: error: cannot declare variable ‘algo_2’ to be of abstract type ‘Reno’
Reno algo_2(1,250);

fukusou.cpp:33:7: note: because the following virtual functions are pure within ‘Reno’:
class Reno:public Base{

fukusou.cpp:22:16: note: virtual void Base::NEW_ssThresh() const
virtual void NEW_ssThresh() const=0;

fukusou.cpp:74:9: error: cannot declare variable ‘algo_3’ to be of abstract type ‘Cubic’
Cubic algo_3(1,250);

fukusou.cpp:41:7: note: because the following virtual functions are pure within ‘Cubic’:
class Cubic:public Base{

fukusou.cpp:22:16: note: virtual void Base::NEW_ssThresh() const
virtual void NEW_ssThresh() const=0;

C++

#include<iostream> #include<string> using namespace std; //基底 class Base{ protected: double cwnd; double ssThresh; public: Base(double CWND,double SSTHRESH):cwnd(CWND),ssThresh(SSTHRESH){}; void Timeout(){ cwnd=1; } void increase_cwnd(){ if(cwnd<ssThresh){ cwnd=cwnd*2; } else{ cwnd=cwnd+1/(cwnd); } } virtual void NEW_ssThresh() const=0; }; //派生 class Tahoe:public Base{ public: Tahoe(double CWND,double SSTHRESH):Base(CWND,SSTHRESH){}; void NEW_ssThresh() { ssThresh=ssThresh; } }; class Reno:public Base{ public: Reno(double CWND,double SSTHRESH):Base(CWND,SSTHRESH){}; void NEW_ssThresh() { ssThresh=cwnd/2; } }; class Cubic:public Base{ public: Cubic(double CWND,double SSTHRESH):Base(CWND,SSTHRESH){}; void NEW_ssThresh() { ssThresh=cwnd*0.8; } }; void simulate(string a[],const Base& algorithm){ int i=0; do{ if(a[i]=="timeout"){ algorithm.Timeout(); } else if(a[i]=="dup_ACK"){ algorithm.NEW_ssThresh(); } else if(a[i]=="ACK"){ algorithm.increase_cwnd(); } }while(a[i]=="0"); } int main(){ string array[50]; for(int i=0;i<50;i++){ array[i]="ACK"; } for(int i=0;i<50;i=i+10){ array[i]="dup_ACK"; } array[25],array[41]="timeout"; Tahoe algo_1(1,250); Reno algo_2(1,250); Cubic algo_3(1,250); simulate(array,algo_1); simulate(array,algo_2); simulate(array,algo_3); }

試したこと

c

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

コンパイル・エラーは解消した。

C++

1#include<iostream> 2#include<string> 3 4//基底 5class Base { 6protected: 7 double cwnd; 8 double ssThresh; 9public: 10 Base(double CWND, double SSTHRESH) :cwnd(CWND), ssThresh(SSTHRESH) {}; 11 void Timeout() { cwnd = 1; } 12 void increase_cwnd() { 13 if (cwnd < ssThresh) { 14 cwnd = cwnd * 2; 15 } else { 16 cwnd = cwnd + 1 / (cwnd); 17 } 18 } 19 virtual void NEW_ssThresh() =0; 20}; 21//派生 22class Tahoe :public Base { 23public: 24 Tahoe(double CWND, double SSTHRESH) :Base(CWND, SSTHRESH) {}; 25 void NEW_ssThresh() override { ssThresh = ssThresh; } 26}; 27 28class Reno :public Base { 29public: 30 Reno(double CWND, double SSTHRESH) :Base(CWND, SSTHRESH) {}; 31 void NEW_ssThresh() override { ssThresh = cwnd / 2; } 32}; 33 34class Cubic :public Base { 35public: 36 Cubic(double CWND, double SSTHRESH) :Base(CWND, SSTHRESH) {}; 37 void NEW_ssThresh() override { ssThresh = cwnd * 0.8; } 38}; 39 40void simulate(std::string a[], Base& algorithm) { 41 int i = 0; 42 do { 43 if (a[i] == "timeout") { 44 algorithm.Timeout(); 45 } else if (a[i] == "dup_ACK") { 46 algorithm.NEW_ssThresh(); 47 } else if (a[i] == "ACK") { 48 algorithm.increase_cwnd(); 49 } 50 } while (a[i] == "0"); 51} 52 53int main() { 54 std::string array[50]; 55 for (int i = 0; i < 50; i++) { 56 array[i] = "ACK"; 57 } 58 for (int i = 0; i < 50; i = i + 10) { 59 array[i] = "dup_ACK"; 60 } 61 array[25], array[41] = "timeout"; 62 Tahoe algo_1(1, 250); 63 Reno algo_2(1, 250); 64 Cubic algo_3(1, 250); 65 simulate(array, algo_1); 66 simulate(array, algo_2); 67 simulate(array, algo_3); 68}

投稿2021/07/06 02:46

episteme

総合スコア16614

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問