Bit stuck: cout with dynamic arrays and c strings

Hello all. First of all thank you for your time and assistance! I'm a c++ newbie (been learning it from scratch for the last couple of days). I've also started learning Python too.

I've got a simple code that creates a dynamic array of characters that a user can add an aditional character too. The code then prints how many elements there are. The final line of my code is to print the array to screen (so in effect printing the characters within the array). However I keep getting a "no match for 'operator<<' error. I've searched around for exactly what this means and how to rectify it but am coming up stumped. The code is below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<char> DynArrChar (3);

    DynArrChar[0] = 'C';
    DynArrChar[1] = 'A';
    DynArrChar[2] = 'T';

    cout << "Number of integers in array: " << DynArrChar.size() << endl;

    cout << "Enter another letter for the array" << endl;

    char AnotherChar = 'A';
    cin >> AnotherChar;
    DynArrChar.push_back(AnotherChar);
    DynArrChar.push_back('\0'); //add terminating character

    cout << "Number of characters in array: " << DynArrChar.size() << endl;
    cout << "Second to last element in array: ";
    cout << DynArrChar[DynArrChar.size() - 2] << endl;

    cout << DynArrChar << endl;

    return 0;
}


To test out what I'm trying to do I've run a simpler code with no issues. Which prints fine:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using namespace std;

int main()
{
    char SayHello[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\0'};
    std::cout << SayHello << std::endl;

    return 0;
}


I suspect there is something about the array in the first that precludes it from being printed to screen with cout?

Thanks again in advance!
DynArrChar is not an array, it's a vector. As the error says, no operator << has been defined where the right hand side operand is of the std::vector<char>. You'll have to define it yourself.
@BangoB,

A vector is different from character array. A vector is not a collection of characters, but a collection of whatever type it was defined as. In order to print out the vector you could try this:
1
2
3
4
for (int i = 0; i < DynArrChar.size(); i++)
{
	cout << DynArrChar[i] << endl;
}


Otherwise everything else appears to work.

hope that helps,

Andy
Thank you everyone! That's cleared it up. It totally slipped past me the obvious that a vector is not an array/matrix.

Thanks again.
Topic archived. No new replies allowed.