编译此代码时,"Error: initialization with '{...}' expected for aggregate object"
在我的函数调用中出现错误。我正在使用 Visual Studio 11 进行编译。
#include <iostream>
#include <string>
#include <array>
using namespace std;
typedef std::array<string, 5> string_t;
string_t centerString(std::array<string, 5> (&tempArray),unsigned short int length);
string_t evenString(std::array<string, 5> (&tempArray),unsigned short int length);
int main(){
std::array<string, 5> aArray = {"a","aa","aaa","aaaa","aaaaa"};
std::array<string, 5> evenStr[5] = evenString(aArray,5);
std::array<string, 5> centerStr[5] = centerStr(aArray,5);
}
string_t evenString(std::array<string, 5> (&srcArray),unsigned short int length){
std::array<string, (sizeof(srcArray)/sizeof(srcArray[0]))> trgtArray= {};
for(unsigned short int x = 0; x < (sizeof(srcArray)/sizeof(srcArray[0]));x++){
trgtArray[x] = srcArray[x];
for(unsigned short int y = 0; y < (length-trgtArray[x].length()); y++){
trgtArray[x] += " ";
}
}
return(trgtArray);
}
string_t centerString(std::array<string, 5> (&srcArray),unsigned short int length){
std::array<string, (sizeof(srcArray)/sizeof(srcArray[0]))> trgtArray= {};
unsigned short int remainder;
string spacer = "";
for(unsigned short int x = 0; x < (sizeof(srcArray)/sizeof(srcArray[0]));x++){
remainder = length - srcArray[x].length();
if((remainder % 2) == 0){
trgtArray[x] = srcArray[x];
for(unsigned short int y = 0; y < (length-srcArray[x].length()); y++){
trgtArray[x] += " ";
}
}else{
for(unsigned short int z = 0; z < (remainder/2); z++){
spacer += " ";
}
trgtArray[x] = spacer + srcArray[x] + spacer + " ";
}
}
return(trgtArray);
}