0

so basicly what i have so far is a code that accesses a classes methods through an object then assigns new values but it keeps crashing everytime i try running can someone please tell me what im doing wrong

#include <iostream> 
#include <fstream>
#include <string> 
using namespace std; 
class person
{
      private:
              string name;
              string age;
              string country;
              string food;
              string sex;
              string drink;
      public:
             string setname(string input){name=input;}
             string setage(string input){age=input;}
             string setcountry(string input){country=input;}
             string setfood(string input){food=input;}
             string setsex(string input){sex=input;}
             string setdrink(string input){drink=input;}

             string showname(){return name;}
             string showage(){return age;}
             string showcountry(){return country;}
             string showfood(){return food;}
             string showsex(){return sex;}
             string showdrink(){return drink;}
};
int main() 
{ 
  //ofstream logwrite("test.txt"); 
  cout<<"Create Person? (y/n) ";
  string anwser;
  cin>>anwser;
  if (anwser=="y")
  {
  person object;
  cout<<"Name? ";
  string name;
  cin>>name;
  object.setname(name);
  cout<<object.showname();

  }
  //logwrite.close(); 
  return 0; 
}

sorry I was in kind of a rush when I wrote this my class ended and i didnt have another spare, Im using Dev-C++ as a compiler so i dont know if it supports warnings or anything but it produces these error messages

C:\Users\13276\Desktop\Untitled3.cpp In member function `std::string person::setname(std::string)':

C:\Users\13276\Desktop\Untitled3.cpp In member function `std::string person::setname(std::string)':

F:\Makefile.win [Build Error] [C:/Users/13276/Desktop/Untitled3.o] Error 1

Jim Jones
  • 2,568
  • 6
  • 26
  • 43
  • 2
    what error message are you getting from the crash? – Jaloopa Mar 07 '13 at 17:17
  • None of your set methods are returning a value, but you are specifying they do. – Overv Mar 07 '13 at 17:17
  • 1
    You need to post your error message and ideally research it first. This is not a good question and we cannot (or should not) help you without that. – djechlin Mar 07 '13 at 17:18
  • 2
    Is your shift key broken? – Etienne de Martel Mar 07 '13 at 17:18
  • `C==` must be a strange language – Bartek Banachewicz Mar 07 '13 at 17:18
  • change all of the methods above to type void, as they don't return anything – naxchange Mar 07 '13 at 17:21
  • 2
    Make sure that your compiler has **warnings enabled**. Every programmer makes silly mistakes, and reading the compilation warnings is a good first measure to correct them. – Drew Dormann Mar 07 '13 at 17:22
  • @DrewDormann I find it strange that the compiler even allows this. The Visual Studio compiler throws an error here. – Overv Mar 07 '13 at 17:23
  • 2
    @Overv: It is not required to put a return statement in a function (because the function may exit from exception or early return). But not returning a value from a method that has a result type is `undefined behavior`. If you turn warnings on the compiler tells you about it. If you tell the compiler to treat warnings as errors (because warnings are really logical errors in your code) then the compiler will not let the above compile. – Martin York Mar 07 '13 at 17:26

3 Answers3

3

So it looks like you have all the setters declared as returning string but you do not do so. I am guessing you meant void:

void setname(string input){name=input;}

Just for completeness sake not returning a value is undefined behavior. If you update all the setters to return void then it looks like that fixes the problem.

As mentioned in the comments if you had warnings turned out you would have probably seen the problem right away. Something along these lines:

Compilation finished with warnings:
source.cpp: In member function 'std::string person::setname(std::string)':
source.cpp:15:54: warning: no return statement in function returning non-void [-Wreturn-type]
Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
2

Your program is crashing because it tries to clean up a string object that doesn't exist.

In your set methods, you are not returning a value (even though your method declaration says it should be). This is undefined behavior:

string setname(string input){name=input;}

However, the generated code will try to deallocate a string object and the free call will fail, because it tries to free some random memory address left on the stack:

*** glibc detected *** ./prog: free(): invalid pointer: 0x08049110 ***
======= Backtrace: =========
/lib/i386-linux-gnu/i686/cmov/libc.so.6(+0x6e3f1)[0xb76103f1]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(+0x6fc58)[0xb7611c58]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(cfree+0x6d)[0xb7614d0d]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x1f)[0xb77904bf]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xb75b8e46]
./prog[0x8048b21]

As seen here. You can solve the problem by changing the return type to void:

void setname(string input) { name = input; }
Overv
  • 8,433
  • 2
  • 40
  • 70
2

You are missing a return statement in setname().

Try either of these instead:

string setname(string input){return name=input;}

void setname(string input){name=input;}

You have similar bugs in your other setxxx functions.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308