lilzz
Posts: 411
Joined: Sat Nov 30, 2013 5:27 pm

Trying to output std::Vector <Class1*> MyClass

Sat Dec 19, 2015 8:00 pm

class1 has a member m_name

Since std:vector <class1*> Myclass is a vector of pointer to a class,
Myclass=the element of the vector, which is a pointer a class1
Myclass->m_name =gets the N_name of class1.


I trying output the m_name

for ( int i = 0; i < Myclass.size(); i++) {
std::cout << (Myclass)->m_name << "\n ";
}

this cause a segmentation fault.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26660
Joined: Sat Jul 30, 2011 7:41 pm

Re: Trying to output std::Vector <Class1*> MyClass

Sat Dec 19, 2015 8:37 pm

Check how you inserted the items in the vector.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: Trying to output std::Vector <Class1*> MyClass

Sat Dec 19, 2015 9:34 pm

Also, how did you create the pointers that you inserted into the vector? Did you create the pointer via a call to new or did you take the address of some object?

Return to “C/C++”