2

When compiling this code I get the error, "Error: initialization with '{...}' expected for aggregate object" at my function calls. I am compiling using 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);
 }
Brook Julias
  • 2,085
  • 9
  • 29
  • 44

2 Answers2

2

You are returning temporary reference in centerString

string_t& centerString(std::array<string, 5> (&tempArray),unsigned short int length);

centerStr is array, can't use it as function

std::array<string, 5> centerStr = centerStr(aArray,5);

I guess you mean

std::array<string, 5> centerStr = centerString(aArray,5);

You could try below sample function, just return array out, RVO should kick in.

std::array<string,5> evenString(std::array<string, 5> (&srcArray), unsigned short length){
     std::array<string,5> trgtArray = {};
    //....
     return trgtArray;
}  

std::array<string, 5> evenStr = evenString(aArray,5);
std::array<string, 5> centerStr = centerString(aArray,5);
billz
  • 44,644
  • 9
  • 83
  • 100
0

Don't ever use native arrays, they suck horrifically, and this is one of the ways in which they suck. Basically, this would require that arrays act sanely and they do not. Language-enforced bad basically means that they can't do a whole host of things that you would expect from any other data type, and they're really not worth the time to use when you could just use std::array like a baus sane person.

Use std::array and be grateful that even the Committee accepts the suckage of C arrays.

I, interestingly, note that you did indeed include <array>, but then did not use std::array.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • I have since changed it to use `#include ` and the problem persists. – Brook Julias Dec 03 '12 at 23:14
  • Where was he passing or returning an array? – Mooing Duck Dec 03 '12 at 23:14
  • 1
    Adding the include doesn't *do* anything. You have to go and use the template defined there, `std::array`. – Puppy Dec 03 '12 at 23:14
  • Sometimes arrays are all you need, saying "don't ever use them" is just overly dogmatic and plain silly. Of course there are valid use cases for arrays (not saying that's the case here though). In this case a `std::array` would still be wrong as the OP is returning a reference to a local. – Ed S. Dec 03 '12 at 23:19
  • @BrookJulias, To get you started: `std::array myArray{{1, 3, 5, 7, 9}}; std::array &someRef = myArray; std::cout << someRef.size();`. It's pretty intuitive. – chris Dec 03 '12 at 23:19
  • I realize it is bad practice to use `using namespace std;`, but it is how I am being taught at the moment. If my understanding of `using namespace std;` is correct you do not then need to use std::array. Though as I am still learning I might have misunderstood this. – Brook Julias Dec 03 '12 at 23:20
  • @BrookJulias, That using statement just saves you from typing `std::` in front of `array`. You still need the `array` part. It's a separate type. – chris Dec 03 '12 at 23:21
  • I just made the changes to the code as you had suggested, but still the error persists. – Brook Julias Dec 03 '12 at 23:26