-3

So I'm still learning C++ and I can't figure out what I am doing wrong. I am using the Code::Blocks IDE and despite including the vector and array header it gives me a big list of errors and says nothing was declared in this scope. The code is very basic:

#include <iostream>
#include <vector>
#include <array>
#include <string>

using namespace std;

int main()
{
string b = const, 10;
vector<string> string1(b);
array<string, b> string2;
return 0;
}

Ok, for the record, THIS IS WHAT I WAS TRYING TO DO:

"Declare a vector object of 10 string objects and an array object of 10 string objects. Show the necessary header files and don’t use using. Do use a const for the number of strings."

1 Answers1

3

You errors are, line by line...

string b = const, 10;

That one simply makes no sense, I don't know what you were expecting there. Assign a string to b instead.

string b = "whatever";

vector<string> string1(b);

vector<T> contains no constructor that takes a T. In your case, no constructor which takes a string. If your compiler supports it, you could use:

vector<string> string1 = {b};

If it doesn't, easy enough to just...

vector<string> string1;
string1.push_back(b);

array<string, b> string2;

array<T,size_t> takes a T (string) and the number of elements, a size_t, as its template arguments. b is a string, not a size_t, so this makes no sense. Pass the size as the second template argument.

array<string, 10> string2;

Per @Benjamin Lindley's comment, perhaps you meant to declare a const int with a value of 10 in that first line. If so, then...

int main()
{
    const int b = 10;
    vector<string> string1(b);
    array<string, b> string2;
    return 0;
}
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • I think perhaps the intent of the first line was to declare a `const` integer to use for the size of both the vector and the array. That would mean that the only thing that needs to be corrected is the first line. I have no idea how `string` got in there though. – Benjamin Lindley Apr 29 '14 at 00:18