I was wandering what could be the causes of this error message? Like in general.\\: *stopped,reason="signal-received",signal-name="SIGSEGV",signal-meaning="Segmentation fault",frame={addr="0x00402d53",func="Domain::Movie::show",args=[{name="this",value="0x1"}],file="..\\src\\Domain\\Movie.cpp",fullname="e:\\eclipse for c++\\workspace\\lb6-8oop\\src\\domain\\Movie.cpp",line="36"},thread-id="1",stopped-threads="all"
10x.
Lucaci Andrew 140 Za s|n
mitrmkar 1,056 Posting Virtuoso
Lucaci Andrew 140 Za s|n
But this I don't understand:
I'll put some code here to see. I have these classes conected through each other by pointers.
/*
* A.cpp
* A.cpp is licensed under GNU GENERAL PUBLIC LICENSE
* Created on: May 7, 2012
* Author: sin
*/
#include "A.h"
#include <iostream>
using namespace std;
A::A() {}
void A::setid(int id){
this->id=id;
}
void A::setname(string n){
name=n;
}
void A::setage(int age){
this->age=age;
}
int A::getage(){
int a=age;
return (a);
}
string A::getname(){
string a=name;
return (a);
}
int A::getid(){
int a=id;
return (a);
}
void A::show(){
cout<<"Id: "<<id<<endl;
cout<<"Age: "<<age<<endl;
}
A::~A() {
// TODO Auto-generated destructor stub
}
/*
* A.h
* A.h is licensed under GNU GENERAL PUBLIC LICENSE
* Created on: May 7, 2012
* Author: sin
*/
#ifndef A_H_
#define A_H_
#include "string"
using namespace std;
class A {
private:
int id, age;
string name;
public:
A();
void setid(int id);
void setage(int age);
void setname(string name);
int getage();
int getid();
string getname();
void show();
virtual ~A();
};
#endif /* A_H_ */
/*
* B.h
* B.h is licensed under GNU GENERAL PUBLIC LICENSE
* Created on: May 7, 2012
* Author: sin
*/
#ifndef B_H_
#define B_H_
#include "A.h"
#include <vector>
#include <iostream>
using namespace std;
class A;
class B {
private:
vector<A> list;
vector<A>::iterator it;
public:
B();
void add(int id, string name, int age);
void show();
void sh();
void del(int pos);
void find(int id);
int getid(int pos);
string getname(int pos);
int getage(int pos);
int getsize();
void findstr(string str);
virtual ~B();
};
#endif /* B_H_ */
/*
* B.cpp
* B.cpp is licensed under GNU GENERAL PUBLIC LICENSE
* Created on: May 7, 2012
* Author: sin
*/
#include "B.h"
#include "iostream"
using namespace std;
B::B() {
// list.resize(15);
}
void B::add(int id, string name, int age){
A *a;
a=new A;
a->setid(id);
a->setname(name);
a->setage(age);
list.push_back(*a);
}
int B::getid(int pos){
int a=list[pos].getid();
return (a);
}
string B::getname(int pos){
string a=list[pos].getname();
return (a);
}
int B::getage(int pos){
int a=list[pos].getage();
return (a);
}
int B::getsize(){
int a=list.size();
return (a);
}
void B::show(){
int i=1;
for ( it = list.begin(); it != list.end(); ++it ){
cout<<"Position: "<<i<<endl;
it->show();
i++;
}
// cout<<list.begin()+1;
}
void B::del(int pos){
list.erase(list.begin()+pos);
}
void B::sh(){
cout<<it->getage();
cout<<"\n\n\n\n\n\n\n\n";
for(int i=0;i<5;i++){
cout<<"Age of pos "<<i<<" is: "<<list[i].getage()<<endl;
}}
void B::find(int id){
for (int i=0;i<(int)list.size();i++){
if(id==list[i].getid()){
cout<<"\ngasit la pos "<<i<<endl;
break;
}
else{
cout<<"\nnegasit la pos "<<i<<endl;
}
}
}
void B::findstr(string str){
for (int i=0;i<(int)list.size();i++){
if(str==list[i].getname()){
cout<<"\ngasit la pos "<<i<<endl;
break;
}
else{
cout<<"\nnegasit la pos "<<i<<endl;
}
}
}
B::~B() {
// TODO Auto-generated destructor stub
}
/*
* C.h
* C.h is licensed under GNU GENERAL PUBLIC LICENSE
* Created on: May 8, 2012
* Author: sin
*/
#ifndef C_H_
#define C_H_
#include "B.h"
class B;
class C {
private:
B* b;
public:
C();
void add(int id, string name, int age);
void show();
void sh();
void del(int pos);
void find(int id);
void findstr(string str);
virtual ~C();
};
#endif /* C_H_ */
/*
* C.cpp
* C.cpp is licensed under GNU GENERAL PUBLIC LICENSE
* Created on: May 8, 2012
* Author: sin
*/
#include "C.h"
C::C() {
// TODO Auto-generated constructor stub
}
void C::add(int id, string name, int age){
b->add(id, name, age);
}
void C::show(){
for (int i=0;i<b->getsize();i++){
cout<<"Id: "<<b->getid(i)<<endl
<<"Name: "<<b->getname(i)<<"\n"
<<"Age: "<<b->getage(i)<<endl;
}
}
void C::sh(){
b->show();
}
C::~C() {
// TODO Auto-generated destructor stub
}
/*
* D.cpp
* D.cpp is licensed under GNU GENERAL PUBLIC LICENSE
* Created on: May 8, 2012
* Author: sin
*/
#include "D.h"
D::D() {
// TODO Auto-generated constructor stub
}
void D::add(){
int id, age;
string name;
cout<<"Id: ";
cin>>id;
cout<<"Name: ";
cin>>name;
cout<<"Age: ";
cin>>age;
c->add(id, name, age);
}
void D::show(){
c->show();
}
void D::sh(){
c->sh();
}
D::~D() {
// TODO Auto-generated destructor stub
}
/*
* D.h
* D.h is licensed under GNU GENERAL PUBLIC LICENSE
* Created on: May 8, 2012
* Author: sin
*/
#ifndef D_H_
#define D_H_
#include "C.h"
class C;
class D {
private:
C* c;
public:
D();
void add();
void show();
void sh();
virtual ~D();
};
#endif /* D_H_ */
/*
* mn.cpp
* mn.cpp is licensed under GNU GENERAL PUBLIC LICENSE
* Created on: May 7, 2012
* Author: sin
*/
#include "D.h"
#include <string>
class D;
int main(){
D* d;
d->add();
d->sh();
// d->show();
return (0);
}
and I keep getting errors, or memory segmentation, or something bad. Why is that? Can any1 figure it out? because I'm working on a bigger project, and I have triangulated the problem @ this pointer links between classes, and came up with this small version.
gusano79 247 Posting Shark
Here's your problem:
int main(){
D* d;
d->add();
You've declared D* d
, but you never assign it a value. It could be pointing anywhere. Then you go and try to use this uninitialized pointer. That is an excellent way to get a segfault.
I compiled this using GCC, and it said "warning: 'd' is used uninitialized in this function". Do you get a warning like this? If it does, you need to pay more attention to your compiler. If it doesn't, get a new compiler.
This is not the only place you're using uninitialized pointers; I count at least two more.
Bonus comment:
#include "A.h"
#include <vector>
#include <iostream>
using namespace std;
class A;
class B {
In B.h
, when you include A.h
, you're including the definition for the A
class already. No need to declare class A;
after that. Again, there are a few places you're doing this.
Lucaci Andrew 140 Za s|n
could you please highlight those parts? i'm too tired to try to find them, if not it's ok.
and yes i got the warning: \mn.cpp:14:13: warning: 'd' is used uninitialized in this function [-Wuninitialized]
and how can i make them to point right?
later edit:
you mean like this? to asign value? d=new D;
Edited by Lucaci Andrew
DeanMSands3 69 Junior Poster
In regards to your redundant class X;, remove them from the beginning of each header not counting A.h. You're including the header files so you don't need an additional class statement.
In regards to the run-time error: yes, exactly. A pointer is not the object it points to. It's like having a mailbox without a house.
#include "D.h"
#include <string>
class D;
int main(){
D* d=new D(); //Add Me!
d->add();
d->sh();
// d->show();
return (0);
}
EDIT: Credit goes to gusano79 whose post I've already up-voted.
Edited by DeanMSands3
histrungalot 32 Junior Poster
When you get to it,
void B::add(int id, string name, int age){
A *a;
a=new A;
a->setid(id);
a->setname(name);
a->setage(age);
list.push_back(*a);
}
is leaking memory.
You define list as vector<A> list;
and in the add function allocate memory for an A class but when you add it to the list it makes a copy of the 'a' class you newed and at that point you have lost the ablity to delete that memory of where 'a' was pointing. You might want to make list be a vector of pointers vector<A*> list;
or have add create A off the stack and then when you push it into the list it gets copied and then no memory leak. I would to the vector of pointers.
DeanMSands3 commented: Good call! +4
gusano79 247 Posting Shark
could you please highlight those parts? i'm too tired to try to find them
Apart from mn.cpp
, have a look at C::C()
and D::D()
; that's where you should be creating the new B
and C
, respectively.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.