-3

I'm beginning with c++ and I don't understand how to solve this issue :

Image

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

int main(int argc, const char * argv[]) {
    //std::cout << "Hello, World!\n";


    string str ("Teststring");
    cout << str.end() << endl;//HERE

Thanks

Kalana
  • 5,631
  • 7
  • 30
  • 51
Oath
  • 1
  • 1
  • 1
    What are you trying to do? Do you really need to know the *ending iterator* of a string? – Thomas Matthews Nov 02 '19 at 17:26
  • 1
    `std::string::end` returns an iterator, for which no `operator <<` overload exists. If you want to print the last character in the string try `std::string::back`. – Unimportant Nov 02 '19 at 17:26
  • .end() returns const_iterator which cannot be outputted. Also using namespace std directive should not be used as it could cause conflicts in further development. – r3t40 Nov 02 '19 at 17:27
  • The way to solve this is to *learn C++* - It may take a few years, but you'll get there. I recommend starting with [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jesper Juhl Nov 02 '19 at 17:27
  • If you want to know the memory location which `str.end()` points to then `cout << (void *)s.end()._Ptr << endl;` – srt1104 Nov 02 '19 at 17:35
  • ok thanks for all the comments. – Oath Nov 02 '19 at 19:02

1 Answers1

0
  1. str.end() will give unexpected behaviour - it returns an iterator starting at the character after the end of the string.
  2. Try replacing the last line with std::cout << str.c_str() << endl; and you'll be making progress. See http://www.cplusplus.com/reference/string/string/c_str/ for more. The pointer/iterator is actually unnecessary, so std::cout << str << endl; will work just as well.
AlanK
  • 1,827
  • 13
  • 16
  • 1
    Why would you use `c_str()` here?? – Lightness Races in Orbit Nov 02 '19 at 22:13
  • @LightnessRaceswithMonica: Good question. No good excuse. Old habits, too many years of C, immersion in pre-2005 Cpp, too much legacy CString, BSTR, wchar_t* etc - and not a helluva lot of attention to detail when firing off snap answer to newbie question. I've updated my answer. – AlanK Nov 04 '19 at 05:19