Home creating a simple class and its variables and function keeps returning " segmentation fault (core dumped)", C++
Reply: 0

creating a simple class and its variables and function keeps returning " segmentation fault (core dumped)", C++

user8657
1#
user8657 Published in September 19, 2018, 9:12 am

I am new to c++. Ive been trying to make a class, assign private variables to it and make public funcs to modify and reach those variables later. Program keeps returning "Segmentation fault (core dumped) Process returned 139 (0x8B). I know there are similar topics with this error, but not I could not find any suitable solution there. Thank you for your time.

# include <iostream>
# include <string>

using namespace std;

class cars{
    public:
    string yearinsert(int x) {
        year = x;
    }
    string nameinsert(string y) {
        name = y;
    }
    void nameget() {
    cout << name << endl;
    }
    void yearget() {
    cout << year << endl;
    }

private:
    int year;
private:
    string name;

};


int main(){

cars skoda;

skoda.nameinsert("Skoda");
skoda.yearinsert(2012);

skoda.nameget();
skoda.yearget();

return 0;
};
share|improve this question
  • 5
    Turn on warnings and listen to them – stark Apr 16 at 17:08
  • I'd reccomend taking a look at this article. – 0x5453 Apr 16 at 17:09
  • How are you compiling this? – stark Apr 16 at 17:10
  • except for missing return value on the *insert function there are nothing wrong with the code. but you should pass const & for string – Arkady Godlin Apr 16 at 17:16
  • whoops sorry to mess with your time. didnt know there was such thing as warnings..helpful, thank you. – Denis Krišťák Apr 16 at 17:18

1 Answer 1

active oldest votes
up vote 0 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.566649 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO