让我们说我为一个大小的数组创建了一个模板:
template <int Size>
class SizedArray {
private:
std::vector<int> array_;
public:
SizedArray() {
for (int i = 0; i < Size; ++i) {
array_.push_back(0);
}
}
... // other methods
};
我想定义一个新功能merge
,它会创建一个包含两个SizedArray
s
SizedArray
template<typename SizeA, typename SizeB>
SizedArray<SizeA + SizeB> merge(SizedArray<SizeA> a, SizedArray<SizeB> b) {
// additional code
}
但是,当我尝试编译此代码时,我收到一条错误说&#34;模板参数1无效&#34; (可能是因为返回类型的SizeA + SizeB
部分)。有没有办法创建一个依赖返回类型,它是函数参数的依赖类型的总和?
答案 0 :(得分:3)
package com.ak.gymtimer;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.concurrent.TimeUnit;
public class CountDown extends AppCompatActivity {
long prepTimeInMillis,workTimeInMillis,restTimeInMillis,coolDownTimeInMillis,totalTimeInMillis;
int round,roundCount=0;
TextView typeV,roundV,totalTimeV,typeSet;
CounterClass timer;
CounterClass1 timer1;
long pausemillis,pauseTotalMillis;
int pauseMethodNum;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_count_down);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); typeV =(TextView) findViewById(R.id.countDownTime);
roundV =(TextView) findViewById(R.id.textView15);
totalTimeV =(TextView) findViewById(R.id.totalTimeleft);
typeSet =(TextView) findViewById(R.id.textView12);
Button pauseBt = (Button) findViewById(R.id.pause);
Button cancelBt = (Button) findViewById(R.id.cancel);
Button nextBt = (Button) findViewById(R.id.next);
Button startBt = (Button) findViewById(R.id.start);
prepTimeInMillis = getIntent().getExtras().getLong("prep");
workTimeInMillis = getIntent().getExtras().getLong("work");
restTimeInMillis = getIntent().getExtras().getLong("rest"); coolDownTimeInMillis=getIntent().getExtras().getLong("cooldown");
round = getIntent().getExtras().getInt("round"); totalTimeInMillis=getIntent().getExtras().getLong("total"); System.out.println("Time in Millis");
System.out.println(prepTimeInMillis);
System.out.println(workTimeInMillis);
System.out.println(restTimeInMillis); System.out.println(coolDownTimeInMillis);
System.out.println(round);
System.out.println(totalTimeInMillis);
if (round == 0){
round = 1;
}
roundV.setText("1/" + round);
timer1 = new CounterClass1(totalTimeInMillis,1000);
timer1.start(); preptimeFunc(); pauseBt.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
timer.cancel();
timer1.cancel();
} });
cancelBt.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
timer1.cancel();
timer.cancel(); }
});
nextBt.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
timer.start(); timer1.start(); } });
startBt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { timer = new CounterClass(prepTimeInMillis,pausemillis,pauseMethodNum); timer.start(); timer1 = new CounterClass1(prepTimeInMillis,pauseTotalMillis); timer1.start(); } }); } public void preptimeFunc(){ typeSet.setText("Count Down"); timer = new CounterClass(prepTimeInMillis,1000,1); timer.start(); } public void worktimeFunc(){ typeSet.setText("WorkOut"); roundCount = roundCount + 1; roundV.setText(String.format("%02d",roundCount) + "/" + String.format("%02d",round)); timer = new CounterClass(workTimeInMillis,1000,2); timer.start(); } public void resttimeFunc(){ typeSet.setText("Rest"); timer = new CounterClass(restTimeInMillis,1000,3); timer.start(); } public void cooldowntimeFunc(){ typeSet.setText("Cool Down"); timer = new CounterClass(coolDownTimeInMillis,1000,4); timer.start(); } public class CounterClass extends CountDownTimer { long millis; int methodNum; public CounterClass(long millisInFuture, long countDownInterval,int methodNum1) { super(millisInFuture, countDownInterval); this.millis = millisInFuture; this.methodNum = methodNum1; } @Override public void onFinish() { timer.cancel(); typeV.setText("00:00"); if (methodNum== 1) { worktimeFunc(); } if (methodNum == 2){ resttimeFunc(); } if (methodNum == 3){ if (roundCount < round){ worktimeFunc(); }else { cooldowntimeFunc(); } } } @SuppressLint("NewApi") @TargetApi(Build.VERSION_CODES.GINGERBREAD) @Override public void onTick(long millisUntilFinished) { millis = millisUntilFinished; pausemillis = millis; pauseMethodNum = methodNum; System.out.println(millis); typeV.setText(String.format("%02d",millis/1000)); } } public class CounterClass1 extends CountDownTimer { long millisTotal; public CounterClass1(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); this.millisTotal = millisInFuture; } @Override public void onFinish() { timer1.cancel(); timer.cancel(); typeV.setText("00:00"); totalTimeV.setText("00:00"); } @SuppressLint("NewApi") @TargetApi(Build.VERSION_CODES.GINGERBREAD) @Override public void onTick(long millisUntilFinished) { millisTotal = millisUntilFinished; System.out.println(millisTotal); pauseTotalMillis = millisTotal; int min = (int) (millisTotal/(1000 * 60)); int sec = (int) ((millisTotal/1000)%60); totalTimeV.setText(String.format("%02d",min) + ":" + String.format("%02d",sec)); } } }
和SizeA
是SizeB
s,而不是typename
s。您显然无法添加类型,但如果您将函数的模板参数声明为int
,那么事情就会起作用。