Why I cannot cout a string?
You need to include
#include <string>
#include <iostream>
Cant cout a string
Besides including the <string>
header as mentioned, I think the rest needs to be fixed, like follows:
#include <iostream>
#include <string>
class person {
class address {
public: // <<<<<<<<<<<<<<<
std::string addr;
int pobox;
address() {
addr = "Some Address";
pobox = 200;
}
};
address a;
public: // <<<<<<<<<<<<<<<
void Test() {
std::cout << a.addr;
}
};
int main() {
person p;
p.Test();
return 0;
}
See the Live Demo.
You'll need to make the class members in question publicly visible, to instantiate or call them.
You may declare address
as a nested type of person (at least public
to become useful), though I'm not so sure that this is such a good idea.
The much better approach seems to be declaring a own, self contained address
class outside of person
, and have the latter one contain a member for it:
class address {
public:
address() : addr_("Some Address"), pobox_(200) {}
const std::string& addr() const { return addr_; }
void addr(std::string value) { addr_ = value; }
int pobox() const { return pobox_; }
void pobox(int value ) { pobox_ = value; }
private:
std::string addr_;
int pobox_;
};
class person {
address a_;
public:
void Test() {
std::cout << "Address: " << a_.addr() << std::endl;
std::cout << "PO Box : " << a_.pobox() << std::endl;
}
};
int main() {
person p;
p.Test();
return 0;
}
See another Live Demo.
why I can't cout after using string tokenizer
Sooner or later ptr=strtok(NULL," ")
will return a null pointer. Which you immediately try to output leading to a null-pointer dereference and undefined behavior (and a very probable crash).
I suggest you modify your loop to something like this:
char *ptr = std::strtok(s, " ");
while (ptr != nullptr)
{
std::cout << ptr << '\n';
ptr = std::strtok(nullptr, " ");
}
Note the order in which things are done, and which guarantees that ptr
will never be a null pointer when you output it.
Why can't I dereference and cout this string in C++
On some compilers <iostream>
happens to also include the <string>
header. On other compilers, specifically the Microsoft compiler, it apparently does not. And the I/O operators for strings are declared in the <string>
header.
It is your responsibility to include all the needed headers, even if the code sometimes happen to work anyway.
So the fix is to just add
#include <string>
at the top of the file.
Cout will not print my string
If your cities are numbered 1, 2, 3, then printcities
will be a string containing three characters, of value '\0x01' '\0x02'
and '\0x03'
. This won't print well. If you were trying to get printcities to hold "123", you either need a stringstream, or std::to_string().
Related Topics
Changing the Current Directory in Linux Using C++
Segmentation Fault When Sending Struct Having Std::Vector Member
Computing Length of a C String at Compile Time. Is This Really a Constexpr
Change the Current Working Directory in C++
How to Render an Opengl Frame in C++ Builder
Porting Clock_Gettime to Windows
G++ Linking Order Dependency When Linking C Code to C++ Code
How to Build a Graphical User Interface in C++
Passing a Std::Array of Unknown Size to a Function
How to Compile SQLite with Icu
Missing Lboost_Thread-Mt in Mongodb Cpp Driver (Ubuntu Server X64)
How to Increase Thread Priority in Pthreads
Declaring the Array Size with a Non-Constant Variable
How to Get the Function Pointer of a Built-In Standard Operator
Get the Compiler Options from a Compiled Executable
Converting Multidimensional Arrays to Pointers in C++
Destroy and Then Construct New Object Using the Same Variable