4

Being new to C++ I have tried to created a simple void function within one of my programs in order to display an array. There is however an error as seen in the title. I believe it is a problem in the fact I am trying to call it with an array in a different form than the functions parameters. I am unsure how to amend that.

#include <iostream>
#include <vector>

using namespace std;

void display_array(string arr[]){
    int i;
    for (i = 0; i < sizeof(arr); i++);
        cout<<arr[i];
}

int main()
{
    string current;
    std::vector<string> paths;

    cout<<"Input paths in the form 'AB'(0 to exit)";
    cin>>current;
    while (current != "0"){
        paths.push_back(current);
        cin>>current;
    }
    display_array(paths);
}

Any help is appreciated.

2
  • 1
    If display_array should print an std::vector<std::string>, make the argument const std::vector<std::string>>&. Commented Jan 13, 2015 at 10:42
  • 1
    Note that your for loop has a semicolon on the end which means that the loop will do nothing but increment i and the next line will be executed once. Commented Jan 13, 2015 at 10:45

4 Answers 4

4

The issue is that the function display_array takes a string[] as an argument, but you are passing in a std::vector<std::string>. You can fix this by changing the display_array function to accept a const-reference to a string vector instead of an array:

void display_array(const std::vector<string>& arr) {
    for (auto it = arr.begin(); it != arr.end(); it++)
        cout<<*it;
}

The reason we pass in a const-reference to the vector instead of passing by value is that we are not going to alter the vector and we don't want to copy it. It is good practice to use const whenever you can and think about the cost of copying your arguments.

Sign up to request clarification or add additional context in comments.

Comments

0

The notation of function display_array existed in C before C++ was around and due to the fact that C++ was made backwardly compatible to C, it compiles in C++ too.

Unfortunately it is rather dangerous because intuitively, it leads to beginners making errors like yours.

In reality you could substitute [] fpr a pointer in the function so it takes string*. And the size is also the size of a pointer, not the number of elements in the array which does not get passed in.

Your options are to pass in the pointer and size, or two pointers in a range where the last one is "one past the end of sequence".

If you are using C++03 you have to use &arr[0] to get to the first element. In C++11 you have arr.data() as a method, which is also safe to call when the vector is empty. (Technically &arr[0] is undefined behaviour if the vector is empty, even if you never try to dereference this pointer).

Thus a correction that would allow your code to work in C++03:

void display_array(const string *arr, size_t size )
{
    int i;
    for (i = 0; i < size; i++) // no semicolon here..
       cout<<arr[i];
}

and call it:

if( !paths.empty() )
      display_array( &paths[0], paths.size() );

Comments

0

display_array function takes an array, should take an std::vector

void display_array(std::vector<string> arr) {
    for (auto s : arr)
        std::cout << s;
}

Comments

-2

You should edit your function signature to:

void display_array(vector<string> &arr)

And:

for (i = 0; i < arr.size(); i++)

4 Comments

... and lose the ; at the end of the for line.
That should be a const reference.
@juanchopanza as OP is quite new, it might be better to keep it simple and not add the notion of const
@dmg I beg to differ. A const & is so fundamentally different from a & that learning them early is one of the key C++ skills. Writing & instead of const & in function parameters is actually a serious problem in actual code, something programmers should not even learn at the start.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.