daystudy.h
#ifndef DAYSTUDY_H
#define DAYSTUDY_H
#define LENGTH 10
template <typename T>
int fillArr(T [],int);
template <typename T>
void modifyArr(T [],int);
template <typename T>
void displayArr(const T [],int);
#endif // DAYSTUDY_H
main.cpp
#include <QCoreApplication>
#include <iostream>
#include <string>
#include "daystudy.h"
int main(int argc, char *argv[])
{
bool continueProcess=true;
int choice=0;
float arr[LENGTH];
int realLength=LENGTH;
while(continueProcess){
std::cout<<"菜单:"<<std::endl<<"1,填充数组\t2,修改数组\t3,显示数组"<<std::endl;
while(!(std::cin>>choice)){
std::cin.clear();
char nextChar=std::cin.get();
if(nextChar=='q'||nextChar=='Q'){
continueProcess=false;
break;
}
std::cin.sync();
std::cout<<"输入数字选项!"<<std::endl;
}
if(!continueProcess){
continue;
}
switch(choice){
case 1:
realLength=fillArr(arr,LENGTH);
break;
case 2:
modifyArr(arr,realLength);
break;
case 3:
displayArr(arr,realLength);
break;
default:
std::cout<<"选项错误!"<<std::endl;
}
}
}
daystudy.fun.cpp
#include <iostream>
#include "daystudy.h"
using std::cout;
using std::endl;
using std::cin;
template <typename T>
int fillArr(T arr[],int length){
int realLength=0;
bool continueFill=true;
while(continueFill && realLength<length){
cout<<"输入"<<realLength+1<<"个元素:";
while(!(cin>>arr[realLength])){
cin.clear();
char nextChar=cin.get();
if(nextChar=='q'||nextChar='Q'){
continueFill=false;
break;
}
cin.sync();
cout<<"请输入正确的类型!"<<endl;
}
}
return realLength;
}
template <typename T>
void modifyArr(T arr[],int length){
int elementIndex=0;
bool continueModify=true;
while(continueModify){
cout<<"输入元素索引:";
while(!(cin>>elementIndex)){
cin.clear();
char nextChar=cin.get();
if(nextChar=='q'||nextChar=='Q'){
continueModify=false;
break;
}
cin.ignore(1000,'\n');
cout<<"请输入正确的索引!"<<endl;
}
if(!continueModify){
continue;
}
if(elementIndex>length || elementIndex<0){
cout<<"输入的索引超出范围!"<<endl;
continue;
}
cout<<"输入元素值:";
while(!(cin>>arr[elementIndex])){
cin.clear();
char nextChar=cin.get();
if(nextChar=='q' || nextChar=='Q'){
continueModify=false;
break;
}
cin.ignore(1000,'\n');
cout<<"请输入正确的数值!"<<endl;
}
}
}
template <typename T>
void displayArr(const T arr[],int length){
for(int i=0;i<length;i++){
cout<<arr[i]<<'\t';
}
cout<<endl;
}
下面是报错的内容
D:\QTproject\DayStudy\main.cpp:28: error: undefined reference to `int fillArr(float*, int)'
D:\QTproject\DayStudy\main.cpp:31: error: undefined reference to `void modifyArr(float*, int)'
D:\QTproject\DayStudy\main.cpp:34: error: undefined reference to `void displayArr(float const*, int)'
collect2.exe:-1: error: error: ld returned 1 exit status