An element cannot be NULL. Something either exists or it does not. For pointers, there is a special type of pointer called null pointer, which means that it points to nothing. But you don't have pointers here.
You should probably clarify with your instructor. Chances are, it is a typo error, i.e., your instructor probably meant "empty string" or equivalently in C++, "string of zero length".
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
OK, talked to instructor today and he helped me fix the test:
The output is this:Code:int main() { std::string * myarray[10]; int i = 0; std::string str1 = "B"; std::string str2 = "C"; std::string str3 = "D"; std::string str4 = "E"; std::string str5 = "A"; while (i < 10) { myarray[i] = NULL; ++i; } int currentsize = 4; myarray[0] = &str1; myarray[1] = &str2; myarray[2] = &str3; myarray[3] = &str4; cout << "Array List 1:" << endl; for (i = 0; i < 10; i++) { if (myarray[i] != NULL) { cout << *myarray[i] << "\n"; } } for (int i=0; i<(currentsize - 1); i++) { myarray[i] = myarray[i + 1]; } cout << "Array List 2:" << endl; for (i = 0; i < 10; i++) { if (myarray[i] != NULL) { cout << *myarray[i] << "\n"; } } myarray[0] = &str5; cout << "Array List 3:" << endl; for (i = 0; i < 10; i++) { if (myarray[i] != NULL) { cout << *myarray[i] << "\n"; } } }
$ ./test
Array List 1:
B
C
D
E
Array List 2:
C
D
E
E
Array List 3:
A
D
E
E
I've tried fiddling with the code a bit, but this would always lead to new errors, such as the amount of elements getting lower or the core dumping and, for the life of me, I can't figure out how to shift it to the right.
Oh wait, I finally figured it out. Got the function working!
I'm actually almost done (until the instructor gives us the test file). I just need a bit of help with ToString. It requires the assistance of the friend operator (first time I've heard of a friend operator, let alone built one), and I'm not sure how to set it so the elements will be coma seperated...or how to use the friend operator in ToString.
Are you not providing some way to access an element by index? If you are, then you do not need to declare ToString as a friend. If you are not, then perhaps ToString should be a member function instead: you can always write a non-member wrapper function of the same name if it is desirable.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I think you could end up with something like this:
To me at least, you end up with the same thing no matter where the hard part is.Code:ostream& operator<< (ostream& os, const DynamicStringArray& arr) { // implementation ... the hard part } string DynamicStringArray::ToString() const { ostringstream result; result << *this; return result.str(); }
What laserlight was saying still matters though. You need to implement a way to access all of the elements of the array. When you implement that, then you can focus on outputting a "comma separated list" with, say, a loop.
Last edited by whiteflags; 03-31-2015 at 04:08 PM.
Really quick, what's "*this" exactly?
I think I have an idea how to make this work...
This should be able to access all the non null elements in the ray. And I believe subtracting 1 from "currentsize" in this for statement will remove the comma from the last element, giving usCode:for (i = 0; i < currentsize - 1; i++) { str = myarray[i]; str = str + ","; myarray[i] = &str; }
A, B, C
instead of
A, B, C,
The problem, from my understanding, is this line:
When I tried compiling it, it wasn't allowed. Is there a way to set the string up so it will be the same as an array element? This is my full test code:Code:&str = myarray[i];
And this is the compile error:Code:int main() { std::string * myarray[10]; int i = 0; std::string str; std::string str1 = "A"; std::string str2 = "B"; std::string str3 = "C"; std::string str4 = "D"; while (i < 10) { myarray[i] = NULL; ++i; } int currentsize = 4; myarray[0] = &str1; myarray[1] = &str2; myarray[2] = &str3; myarray[3] = &str4; cout << "Array List 1:" << endl; for (i = 0; i < 10; i++) { if (myarray[i] != NULL) { cout << *myarray[i]; } } for (i = 0; i < currentsize - 1; i++) { str = myarray[i]; str = str + ","; myarray[i] = &str; } cout << "Array List 2:" << endl; for (i = 0; i < 10; i++) { if (myarray[i] != NULL) { cout << *myarray[i]; } } }
Code:$ make /usr/bin/g++ -Wall -Wextra -g test.cpp -o test test.cpp: In function ‘int main()’: test.cpp:36:20: error: invalid conversion from ‘std::string* {aka std::basic_string<char>*}’ to ‘char’ [-fpermissive] str = myarray[i]; ^ In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/string:52:0, from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/locale_classes.h:40, from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/ios_base.h:41, from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ios:42, from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/ostream:38, from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/iostream:39, from test.cpp:1: /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/basic_string.h:573:7: note: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ operator=(_CharT __c) ^ makefile:10: recipe for target 'test' failed make: *** [test] Error 1
Last edited by Erik Ingvoldsen; 04-01-2015 at 01:17 AM.
Consider a program like this:
The idea is that given the container of strings, assuming the container is not empty, you can start off a stringstream with the first string. Then, loop over the container and keep appending a comma followed by the current string. At the end, you can call the str() member function to get the resulting comma-separated value string.Code:#include <iostream> #include <sstream> #include <string> #include <vector> int main() { using namespace std; vector<string> words{"these", "are", "a", "few", "words"}; if (!words.empty()) { stringstream ss; ss << words[0]; for (vector<string>::size_type i = 1; i < words.size(); ++i) { ss << "," << words[i]; } cout << ss.str() << endl; } }
I leave modifying this to ignore empty strings as an exercise. You probably need to be most careful to handle the case where the very first string is empty.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Well, last time I had that issue, it was solved by doing this:
But when I do that hereCode:myarray[i] = &str;
I get this:Code:&str = myarray[i]
Code:$ make /usr/bin/g++ -Wall -Wextra -g test.cpp -o test test.cpp: In function ‘int main()’: test.cpp:36:10: error: lvalue required as left operand of assignment &str = myarray[i]; ^ makefile:10: recipe for target 'test' failed make: *** [test] Error 1