Can anyone tell me why this is segfaulting

Can anyone tell me why this is segfaulting?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>

class myClass
{
public:
myClass(int data):theDataMember(data){howManyInstances++;}
~myClass(){howManyInstances--;}
static int getHowManyInstances(){return howManyInstances;}
float setTheDataMemberTwo(float memberTwo){theDataMemberTwo=memberTwo;}
float setTheDataMemberThree(float memberThree){theDataMemberThree=memberThree;}
int getTheDataMember(){return theDataMember;}
float getTheDataMemberTwo(){return theDataMemberTwo;}
float getTheDataMemberThree(){return theDataMemberThree;}
private:
int theDataMember;
float theDataMemberTwo;
float theDataMemberThree;
static int howManyInstances;
};

int myClass::howManyInstances = 0;

int main()
{


myClass ObjectOne(5);
myClass ObjectTwo(10);
myClass ObjectThree(20);

int (myClass::*fPtr)()=&myClass::getTheDataMember;

std::cout<<"There are : " <<myClass::getHowManyInstances()<< " objects."<<std::endl;



float (myClass::*fPtrTwo)();

ObjectOne.setTheDataMemberTwo(5.5f);
ObjectOne.setTheDataMemberThree(4.4f);

std::cout<<"Object one's data member 2 = " <<(ObjectOne.*fPtrTwo)()<<std::endl;

enum BOOL{FALSE,TRUE};

BOOL fQuit = FALSE;

int choice;

while(fQuit == FALSE)
{
	std::cout << "(0)Quit (!)Function two (2)Function three"<<std::endl;
	std::cin>>choice;
	switch(choice)
	{
	case 1: fPtrTwo=&myClass::getTheDataMemberTwo;
	std::cout<<"The value is : " <<(ObjectOne.*fPtrTwo)()<<std::endl;
	break;
	case 2: fPtrTwo=&myClass::getTheDataMemberThree;
	std::cout<<"The value is : " <<(ObjectOne.*fPtrTwo)()<<std::endl;
	break;
	default : fQuit = TRUE; 
	break;
	}
if(fQuit)
break;
}

}
My compiler wrote:
In member function ‘float myClass::setTheDataMemberTwo(float)’:
warning: no return statement in function returning non-void [-Wreturn-type]

In member function ‘float myClass::setTheDataMemberThree(float)’:
warning: no return statement in function returning non-void [-Wreturn-type]
Last edited on
ok fixed that error. Now can someone shor me the correct syntaxz for declaring the function pointer and having it assigned to two different functions in the switch statement.

#include<iostream>

class myClass
{
public:
myClass(int data):theDataMember(data){howManyInstances++;}
~myClass(){howManyInstances--;}
static int getHowManyInstances(){return howManyInstances;}
void setTheDataMemberTwo(float memberTwo){theDataMemberTwo=memberTwo;}
void setTheDataMemberThree(float memberThree){theDataMemberThree=memberThree;}
int getTheDataMember(){return theDataMember;}
float getTheDataMemberTwo(){return theDataMemberTwo;}
float getTheDataMemberThree(){return theDataMemberThree;}
private:
int theDataMember;
float theDataMemberTwo;
float theDataMemberThree;
static int howManyInstances;
};

int myClass::howManyInstances = 0;

int main()
{


myClass ObjectOne(5);
myClass ObjectTwo(10);
myClass ObjectThree(20);

int (myClass::*fPtr)()=&myClass::getTheDataMember;

std::cout<<"There are : " <<myClass::getHowManyInstances()<< " objects."<<std::endl;



void (myClass::*fPtrTwo)()const=0;

ObjectOne.setTheDataMemberTwo(5.5);
ObjectOne.setTheDataMemberThree(4.4);

std::cout<<"Object one's data member 2 = " <<(ObjectOne.*fPtrTwo)()<<std::endl;

enum BOOL{FALSE,TRUE};

BOOL fQuit = FALSE;

int choice;

while(fQuit == FALSE)
{
std::cout << "(0)Quit (!)Function two (2)Function three"<<std::endl;
std::cin>>choice;
switch(choice)
{
case 1: fPtrTwo=myClass::getTheDataMemberTwo;
std::cout<<"The value is : " <<(ObjectOne->*fPtrTwo)<<std::endl;
break;
case 2: fPtrTwo=myClass::getTheDataMemberThree;
std::cout<<"The value is : " <<(ObjectOne->*fPtrTwo)<<std::endl;
break;
default : fQuit = TRUE;
break;
}
if(fQuit)
break;
}

}
Let the language help you, with auto.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<iostream>

class myClass
{
public:
  myClass(int data):theDataMember(data){howManyInstances++;}
  ~myClass(){howManyInstances--;}
  static int getHowManyInstances(){return howManyInstances;}
  void setTheDataMemberTwo(float memberTwo){theDataMemberTwo=memberTwo;}
  void setTheDataMemberThree(float memberThree){theDataMemberThree=memberThree;}
  int getTheDataMember(){return theDataMember;}
  float getTheDataMemberTwo(){return theDataMemberTwo;}
  float getTheDataMemberThree(){return theDataMemberThree;}
private:
  int theDataMember;
  float theDataMemberTwo;
  float theDataMemberThree;
  static int howManyInstances;
};

int myClass::howManyInstances = 0;

int main()
{


  myClass ObjectOne(5);
  myClass ObjectTwo(10);
  myClass ObjectThree(20);

  auto func1 = &myClass::getTheDataMember;
  auto fPtrTwo=&myClass::getTheDataMemberTwo;

  std::cout<<"There are : " <<myClass::getHowManyInstances()<< " objects."<<std::endl;


  ObjectOne.setTheDataMemberTwo(5.5);
  ObjectOne.setTheDataMemberThree(4.4);

  std::cout<<"Object one's data member 2 = " <<(ObjectOne.*fPtrTwo)()<<std::endl;

  enum BOOL{FALSE,TRUE};

  BOOL fQuit = FALSE;

  int choice;

  while(fQuit == FALSE)
    {
      std::cout << "(0)Quit (!)Function two (2)Function three"<<std::endl;
      std::cin>>choice;
      switch(choice)
	{
	case 1:  fPtrTwo=&myClass::getTheDataMemberTwo;
	  std::cout<<"The value is : " <<(ObjectOne.*fPtrTwo)()<<std::endl;
	  break;
	case 2:  fPtrTwo=&myClass::getTheDataMemberThree;
	  std::cout<<"The value is : " <<(ObjectOne.*fPtrTwo)()<<std::endl;
	  break;
	default : fQuit = TRUE;
	  break;
	}
      if(fQuit)
	break;
    }

}


Last edited on
can someone shor me the correct syntaxz for declaring the function pointer and having it assigned to two different functions in the switch statement.

Without auto the declaration of fPtrTwo would look like this:

 
float (myClass::*fPtrTwo)() = 0;

or

 
float (myClass::*fPtrTwo)() = nullptr;
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es47-use-nullptr-rather-than-0-or-null
Last edited on
I've manged to make some progress with this but can anybody help me make the second part of the loop at the end work? thanks.

#include<iostream>

class myClass
{
public:
myClass(){}
myClass(int data):theDataMember(data){howManyInstances++;}
~myClass(){howManyInstances--;}
static int getHowManyInstances(){return howManyInstances;}
void setTheDataMemberTwo(float memberTwo){theDataMemberTwo=memberTwo;}
void setTheDataMemberThree(float memberThree){theDataMemberThree=memberThree;}
int getTheDataMember(){return theDataMember;}
float getTheDataMemberTwo(){return theDataMemberTwo;std::cout<<"THe data member = "<<theDataMemberTwo<<std::endl;}
float getTheDataMemberThree(){return theDataMemberThree;std::cout<<"THe data member = "<<theDataMemberThree<<std::endl;}
private:
int theDataMember;
float theDataMemberTwo;
float theDataMemberThree;
static int howManyInstances;
};

int myClass::howManyInstances = 0;

int main()
{
myClass ObjectOne(5);
int (myClass::*fPtr)()=&myClass::getTheDataMember;
std::cout<<"There are : " <<myClass::getHowManyInstances()<< " objects."<<std::endl;
float (myClass::*pFunc)();
myClass*ptr=0;
enum BOOL {FALSE,TRUE};
BOOL fQuit=FALSE;
int choice;
int member;

while(fQuit == FALSE)
{
std::cout <<"(0)Quit (2)New myClass" <<std::endl;
std::cin>>choice;
switch(choice)
{
case 1 : ptr = new myClass;
break;
default : fQuit = TRUE;
{
if(fQuit)
break;


std::cout<<"(!)Func two (3)Func three.\n";
std::cin>>member;
switch(member)
{
case 1: pFunc=&myClass::getTheDataMemberTwo;break;
default: pFunc=&myClass::getTheDataMemberThree;break;
}

(ptr->*pFunc)();

delete ptr;
}

}

}

}
can anybody help me make the second part of the loop at the end work?


Which part is that?
the std::cout after if(fQuit) is not showing after i make the new object in the 1st menu, why?
It's inside the default section of the switch block, and that doesn't run if you've created a new object.
Topic archived. No new replies allowed.