1

What i'm trying to do is create a template array class that will store values of a data type into an array. I have it working fine with int values, however working with string objects things start to break down.

I've taken out the block of code and tried it on it's own and I do get the same error. I'm sure I've learnt this, and I'm almost positive that the answer is something simple, trying to wrap my head around the pace in which we're learning c++ is a little crazy at times!

My best guess right now, is that I would need to tokenize the string and look for spaces. I tend to over think things though which lead to more confusion - thus me seeking out a answer here!

The code:

// Test String: Hello World this is a String Object
int stringSize = 7;
int count = 0;

string s[stringSize];

cout << "\nEnter " << stringSize << " one-word string values:\n";

while (count < stringSize) {

    string tmpVal;

    cin >> tmpVal;
    s[count] = tmpVal;

    count ++;
}
3
  • 1
    "I've taken out the block of code and tried it on it's own and I do get the same error." - perhaps you could tell us what the error is. Mar 2, 2010 at 17:22
  • I do not get any errors with above code. It would have helped if you would have stated the error you are getting...
    – DevSolar
    Mar 2, 2010 at 17:25
  • Note: I just added the missing delete[] call to my answer. Don't forget to use delete[] if you do this yourself.
    – Brian
    Mar 2, 2010 at 17:37

8 Answers 8

1

string s[stringSize]; is illegal because stringSize is not a constant. You must either use dynamic memory (i.e. string* s = new string [stringSize];), include stringsize as a template argument (don't do this, it doesn't actually solve the problem), use a fixed size value, or use an existing structure (I'd suggest vector, as in Bill's answer). The code below works fine on my compiler:

int main(int argc, char *argv[]) {
int stringSize = 7;
int count = 0;
string* s = new string [stringSize];
cout << "\nEnter " << stringSize << " one-word string values:\n";
while (count < stringSize) {
    string tmpVal;
    cin >> tmpVal;
    s[count] = tmpVal;
    count ++;
    }
    delete[] s;
}
3
  • string s[7] is perfectly valid. it declares an array of 7 strings string s[val] is valid on gcc which allows dynamically sized stack arrays; his issue is that he has made the array fixed size, it should be a vector
    – pm100
    Mar 2, 2010 at 17:42
  • @pm100: string s[size]; is only valid on gcc because of a compiler extension. It is not portable code, as the OP found out. const int size = 7; string s[size]; is valid C++, assuming vector isn't the correct solution.
    – Bill
    Mar 2, 2010 at 17:52
  • thats what I said. Brian said that string s[stringSize] is invalid becuase 's is a dynamic string', no its not, its a string array on some platforms and wont compile on other platforms. Maybe he meant 'dynamic string array'
    – pm100
    Mar 2, 2010 at 20:16
1

I am a little confused as to exactly what you're looking for, but I suggest looking into the standard library.

Perhaps something like:

list<string> s;

and then, in the loop use push_back.

1

I am also confused what is your actual question, because your code works. However, FWIW, I would suggest the following. The changes are: (1) use of const (already suggested by others), (2) use of size_t, (3) change of variable name stringSize to numStrings (because of this I was confused at first glance), and (4) avoiding string copy.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    const size_t numStrings = 7;
    size_t count = 0;

    string s[ numStrings ];

    cout << "\nEnter " << numStrings << " one-word string values:\n";

    while (count < numStrings) {
        cin >> s[ count ];
        count++;
    }

    return 0;
}
0

Why not read in the entire line, then find all spaces and using the substr method, split the string?

You will need the following methods: getline() find_first_of() substr()

Also, searching around this site for splitting strings in c++ will give you a lot of tips.

1
  • Im using a template class, so I wanted to avoid splitting strings
    – cdnicoll
    Mar 2, 2010 at 17:33
0

First of all, the size of your array should be constant:

const int stringSize = 7;

Secondly, as dbrien said, you should use std::vector unless you're doing this for the learning experience:

std::string tmpVal;
std::vector<std::string> s;
cout << "\nEnter " << stringSize << " one-word string values:\n"; 

while (cin >> tmpVal)
{ 
    s.push_back(tmpVal);
}
0

First, the array dimension must be constant, so it should be const int stringsize = 7; Also, I would suggest using std::vector rather than std::list, additionally What was the error?

0

Not sure what error you're getting, but this is wrong because you need to use a constant integral value to allocate arrays on the stack.. Change:

int stringSize = 7;
int count = 0;

string s[stringSize];

... to:

const int stringSize = 7;
int count = 0;

string s[stringSize];

You can and probably should also use a vector instead of using C-style arrays, or trying to hand roll your own templated array class:

vector<string> s;
const int stringSize = 7;

cout << "\nEnter " << stringSize << " one-word string values:\n";

while (s.size()  < stringSize) {

    string tmpVal;

    cin >> tmpVal;
    s.push_back(tmpVal);
}
-2

So it turns out it was the compiler. I was using xCode and getting:

cin_cout(7307) malloc: *** error for object 0x1000072c0: pointer being freed was not allocated

*** set a breakpoint in malloc_error_break to debug

Running the same block in Visual c++ seemed to be ok... Sorry for my stupidity and thanks kindly for all the quick feedback!

1
  • 4
    Please edit your own post instead of providing an answer to it. Mar 2, 2010 at 17:38

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.