This question already has an answer here:
Error in Getting Value from Vector of Pairs
(1 answer)
Closed 7 years ago.
I am having trouble with printing values from my struct Pages. I am trying to print the value of segment in Pages from a list of pages. The assignSegments() method is assigning values to the variable segment. This is the error I am getting. ‘std::list::iterator’ has no member named ‘segment’
Here is the code I have so far. If you need more code from me just let me know.
Following code is in my main
Process child;
char c = 'A';
c = c+i;
child.letter = c;
assignSegments(child.childPages, child.letter, CreateRandomNumber());
for(list<Pages>::iterator iter = child.childPages.begin(); iter != child.childPages.end(); i++)
{
cout << *iter.segment << endl; // having trouble printing here
}
Here are my structs that I am using.
struct Pages
{
string segment;
char validBit;
int refByte;
Pages * pagePointer;
int* memoryPointer;
};
struct Process
{
char letter;
list<Pages> childPages;
};
I think cout << iter->segment << endl; is what you're looking for.
operator. has a higher precedence over operator*, so iter.segment will be evaluated first, obviously it's not what you want.
Reference for C++ Operator Precedence
you can change
cout << *iter.segment << endl;
to
cout << (*iter).segment << endl;
or just
cout << iter->segment << endl;
Related
This question already has answers here:
How to print class object using operator<<
(3 answers)
Closed 3 months ago.
I created a class called person with two members name and age then I created two objects of that
class p1 and p2 and then I added them to a vector. I tried then to print them but could not.
this my code:
class Person{
public:
string name;
int age;
};
int main(){
Person p;
vector <Person> vector;
p.name = "Vitalik";
p.age = 29;
Person p2;
p2.name = "Bueterin";
p2.age = 50;
vector.push_back(p);
vector.push_back(p2);
for(int i = 0; i < vector.size(); i++){
cout << vector[i] << endl;
}
return 0;
}
I tried multiple ways to loop through the vector and print the elements but I keep getting this message:
error: invalid operands to binary expression ('std::ostream' (aka 'basic_ostream<char>') and 'std::__vector_base<Person, std::allocator<Person> >::value_type' (aka 'Person'))
cout << vector[i] << endl;
You can implement operator<< or just write something like this:
cout << vector[i].name << ": " << vector[i].age << endl;
cout doesn't know how to print this object by default.
you should implement an operator<< method
How to properly overload the << operator for an ostream?
https://en.cppreference.com/w/cpp/language/operators
This question already has answers here:
Two different values at the same memory address
(7 answers)
C/C++ changing the value of a const
(18 answers)
Closed 5 months ago.
friends.
Recently I experimented a bit a C++ constants.
The code is:
#include <iostream>
int main() {
const int c = 1;
const int* ptr = &c;
int* tmp = const_cast<int*>(ptr);
*tmp = 5;
std::cout << &c << " " << ptr << " " << tmp << "\n";
std::cout << c << " " << *ptr << " " << *tmp;
}
I have investigated assembly code at godbolt: https://godbolt.org/z/7e3o7bWrs
The assembly code seems like doing what I wrote, exactly moving address of c into tmp variable and changes variable at this address.
Can you please tell me, why there is could be two different values at the same addresses?
Thank you.
This question already has answers here:
How to use std::find() with vector of custom class?
(2 answers)
Closed last year.
C++ is not really in my skills, so I have vector of objects which is obviously several copies of a class.
My class is named "Contact", and my function I am property passing in my vector objects as reference.
As soon as I try to add in this find, I guess an error
void Contact::searchContactByLastName(string name, vector<Contact>& allContacts) {
cout << "In SearchContactByLastName \n"; //
unsigned int count = allContacts.size();
for (unsigned int i = 0; i < count; i++) {
//this works
cout << " Last Name " << i << " = " << allContacts[i].getLastName() << endl;
// THIS IS WHAT DOES NOT WORK, even outside the for loop ....
if (std::find(allContacts.begin(), allContacts.end(), name) != allContacts.end()) {
// Found the item
}
}
}
Since you probably don't want to implement the == operator to match Contract and std::string, it's a good idea to std::find_if allowing you to pass the matching functionality as parameter.
if (std::find_if(allContacts.begin(), allContacts.end(), [&name](Contract const& contract) {return name == contract.getLastName();}) != allContacts.end()) {
// Found the item
}
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 7 years ago.
I'm trying to learn C++ and had a question about returning arrays in C++. I know that in this case perhaps Vector may be better and that there is no need for a getter method as the fields are visible within the same class but I'm trying to learn about memory management so I'll use them.
class Color {
double r;
double g;
double b;
public:
Color(int a, int aa, int aaa) {
r = a;
g = aa;
b = aaa;
}
bool operator==(const Color &other) {
double *otherCol = other.getter();
return otherCol[0] == r && otherCol[1] == g && otherCol[2] == b;
}
double* getter() const {
double second[3] = {r,g,b};
return second;
}
};
int main() {
Color col1(23, 54, 200);
Color col2(23, 54, 200);
cout << (col1 == col2) << endl;
return 0;
}
This code should print out a 1 if the RGB colors are the same and 0 otherwise. But it does not print a 1. To debug, I added the following lines (twice on purpose) right before the return statement in operator==:
cout << otherCol[0] << " " << otherCol[1] << " " << otherCol[2] << endl;
cout << otherCol[0] << " " << otherCol[1] << " " << otherCol[2] << endl;
Oddly enough, the results are different:
23 54 200
6.91368e-310 6.91368e-310 3.11046e-317
Can someone please tell me what causes this and what would be a reasonable remedy that does not rely on Vector or dynamic allocation of memory? Let's assume that we don't want to pass in an array into getter() to update.
In getter you are returning the address of a local variable which results in undefined behavior so you are getting random memory. If you return a pointer you have to do something like new the variable in the called function and delete it after the function returns.
Or you could make second a class member which would keep it from going out of scope.
You could also pass the array in as a parameter.
This question already has answers here:
Function argument type followed by *&
(3 answers)
Closed 9 years ago.
I was looking at manual of ACE framework and came across this declaration
int ACE_Stream<>::get (ACE_Message_Block *& mb, ACE_Time_Value * timeout = 0)
I'm not able to understand what *& stands for. I know * is for pointer and & is reference. Can any one explain what is the meaning of this declaration.
Thanks in advance
So as #NPE said *& makes changes to pointer propagate back. But to understand I just wrote down some code sharing it so that it can help others understand this correctly
#include <iostream>
using namespace std;
class DoSomething
{
public:
int n;
DoSomething(int i){
n = i;
}
virtual ~DoSomething();
};
DoSomething::~DoSomething()
{
}
int dosomething(DoSomething * a)
{
cout << "Got value from caller: (in dosomething) = " << a << endl;
a = new DoSomething(25);
return 0;
}
int dosomethingElse(DoSomething *& a)
{
cout << "Got value from caller: (in dosomethingElse) = " << a << endl;
a = new DoSomething(15);
return 0;
}
int main(int argc, char *argv[])
{
DoSomething *d = new DoSomething(10);
cout << "Pointer to DoSomething: " << d << endl;
dosomething(d);
cout << "After dosomething value of d: " << d << endl << endl;
dosomethingElse(d);
cout << "After dosomethingElse value of d: " << d << endl << endl;
delete d;
return 0;
}
So as #NPE said here is out put of this
Pointer to DoSomething: 0x955f008
Got value from caller: (in dosomething) = 0x955f008
After dosomething value of d: 0x955f008
Got value from caller: (in dosomethingElse) = 0x955f008
After dosomethingElse value of d: 0x955f028
So indeed if I create a new instance inside function it will propagate only if I use *& and not just *
Thank you to every one for the answers.
I know * is for pointer and & is reference.
Correct. So what you have here is a pointer, passed by reference.
mb is a pointer that's being passed by reference. This means that if get() were to change the value of the pointer, the change would propagate back to the caller.