This question already has answers here:
uint8_t can't be printed with cout
(8 answers)
Closed 4 months ago.
#include<iostream>
#include<string>
class Person_t{
private:
uint8_t age;
public:
void introduce_myself(){
std::cout << "I am " << age << " yo" << std::endl;
}
Person_t()
: age{99}
{ };
};
int main(){
Person_t person1{};
person1.introduce_myself();
}
When the shown code is executed, the integer from the initializer list gets converted to a c. I have no explaination why, could someone please explain this to me?
<< age
age is a uint8_t, which is an alias for an underlying native type of a unsigned char. Your C++ library implements std::ostream's << overload for an unsigned char as a formatting operation for a single, lonely, character.
Simply cast it to an int.
<< static_cast<int>(age)
Related
This question already has answers here:
Unsigned keyword in C++
(5 answers)
Closed 4 years ago.
What happens if we do not put an int or char after unsigned and signed variables?
#include<iostream>
using namespace std;
int main()
{
unsigned u = 10; // how is this declaration helpful?
signed s = 12; // is there any use with such declarations without specifying a primitive datatype
cout << u << " " << s << endl; // prints the values of the variables as usual
return 0;
}
signed and unsigned implicitly mean signed int and unsiged int, respectively.
This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Printing C++ int pointer vs char pointer
(3 answers)
Closed 4 years ago.
Keep in mind that my knowledge of pointers is quite small, as I just started learning about them.
While I was messing around in C++, I wrote this small bit of code thinking it would just print out the address of each character in the string
#include <iostream>
using namespace std;
string a = "Hello, World!";
int main() {
for(int i=0; i<a.length();i++) {
cout << &a[i] << endl;
}
return 0;
}
When I compiled and ran this, however, it resulted in it printing as if the string moved to the left.
It just doesn't make sense why when it uses &, which I thought would retrieve the address, would instead get the rest of the string.
As said in the comment, &a[i] is a pointer to a char, and << operator will print null terminated string starting from this character, not its address. So if you want to print the address, you must cast it to void *, as follow :
#include <iostream>
using namespace std;
string a = "Hello, World!";
int main() {
for(int i=0; i<a.length();i++) {
cout << (void *)&a[i] << endl; //cast to (void *) to get the address
}
return 0;
}
string subscript ([]) operator returns char. So & operation returns a pointer to char. And cout operator<< has an overloading for it, which consider it should print out the parameter as a c-string. You should cast it to void* so cout wouldn't think it is a string.
(void*)&a[i]
This question already has answers here:
Printing array element memory adresses C and C++, why different output? [duplicate]
(4 answers)
Closed 6 years ago.
Refer the code below:
#include <iostream>
class Boy {
char name[10];
public:
void show() {
*name = 0;
std::cout << "\n" << &name[0];
}
};
int main() {
Boy b;
b.show();
}
Here, why don't we see the address of name[0]. I also tried with name, which itself is address. Still I can't see the address, it returns blank screen.
It's because you're using char* overload for operator<<, which treats the pointer as a pointer to c-string. Cast your pointer to void* to print it as such.
std::cout << "\n" << static_cast<void*>(&name[0]);
This question already has answers here:
Cannot understand the error msg: 'nonstatic member reference must be relative to specific object' [closed]
(2 answers)
Closed 8 years ago.
My Source Code:
#include <iostream>
#include <string>
int main(void) {
struct character{
std::string name;
unsigned short int age;
unsigned short int height;
unsigned short int weight;
};
std::cout << "==============================================\n";
std::cout << "Welcome to Jake's Character Console! (JCC v1.0)\n";
std::cout << "==============================================\n\n";
std::cout << "Let's start by describing your character..." << std::endl << std::endl;
std::cout << "What is your character's name? ";
std::cin >> character.name; \\ <======= ERROR HERE ========
std::cout << "Let's start by describing your character..." << std::endl << std::endl;
std::cout << "Let's start by describing your character..." << std::endl << std::endl;
std::cin.get();
std::cin.get();
return 0;
}
The Problem:
The error occurs at the 'std::cin >> character.name;' statement. I am a complete, absolute novice at C++. I was making this program to learn the ropes of data structures, but I ran across this error. How could I simply rewrite this code so that I could input data into the character.name member? Also, any expert advice would be much appreciated; I don't have much C++ prior knowledge. Thank you SOF community.
The Error:
'A non-static member reference must be relative to a specified object.'
character is the name of the class (i.e it is a type). What you're doing is equivalent to cin >> int.name which makes no sense because int is a type and a keyword, not an object.
Note: Not to say that character is an integer. Those are two different types in and of themselves. Another difference is that character is a class type and int is a built-in non-class type.
You need to create an object of type character which you can use. You do that by doing:
character myChar;
Then later on you do:
std::cin >> myChar.name;
You need to declare an instance of the struct character:
character c;
c.name = "Cake";
std::cout << c.name << std::endl;
//=> "Cake"
You should likely be declaring a struct outside of the main function. However, you can create an instance of the class like this:
struct character{
std::string name;
unsigned short int age;
unsigned short int height;
unsigned short int weight;
}character;
If you are going to do this, I would change the names around. This code is legal but may be confusing. This syntax seems to be used more in C.
This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 5 years ago.
Why does
#include <iostream>
using namespace std;
int main() {
cout << (char*)0x10 << endl;
}
segfault, but
#include <iostream>
using namespace std;
int main() {
cout << (void*)0x10 << endl;
}
seems to work just fine?
Because
cout::operator <<(void*)
prints a memory address, and
cout::operator <<(char*)
prints a null-terminated character array, and you run into undefined behaviour when you attempt to read the char array from 0x10.
The ostream::operator<< is overloaded, there is a version for char* which interprets the given pointer as a null-terminated string.
There's a special overload for << with char*, so that C-style strings can be output easily.
Thus
cout << (char*)0x10 << endl;
tries to print out the string located at (char*)0x10 which is not memory it's supposed to look at.