Class help

I wanna ask why my program terminated with a class like this:
1
2
3
4
5
6
7
8
9
10
11
class Stop{
public:
  Stop(){Aid=0; parent_count=0};
  int next;
  int parent_count;
  bool able;
  void add_parent(int a);
private:
  bool Aid;
  vector<int> parents;
};

And it won't be terminated without int parent_count;.
And it won't be terminated without int parent_count;

Can you show us your full code?
I try to run and compile the following code(both with and without int parent_count):
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
#include<iostream>
#include<vector>

using namespace std;

class Stop{
public:
  Stop(){Aid=0; parent_count=0;};
  int next;
  int parent_count;
  bool able;
  void add_parent(int a);
private:
  bool Aid;
  vector<int> parents;
};

void Stop::add_parent(int a){
  parents.push_back(a);
}

#ifndef MAX
#define MAX 100000
#endif

int main(int argc, char const *argv[]) {
  int N, M, K;
  Stop stop[MAX];
  stop[1].next=0;
  stop[1].able=1;
  cin>>N>>M>>K;
  cout<<N<<endl;
  return 0;
}

Of course I make sure to delete parent_count=0 as well.
You create some Stop objects.

The constructor for each stop object sets parent_count=0.

Unless you declared a parent_count variable then the constructor wouldn't work.

What do you mean by "terminated"?
Last edited on
@lastchance
Yes I did declare parent_count as an int.
"Terminated" as I run the program that I compiled. It just stopped without me input anything.
closed account (48T7M4Gy)
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
#include<iostream>
#include<vector>

using namespace std;

class Stop{
public:
    Stop(){Aid=0; parent_count=0;};
    int next;
    int parent_count;
    bool able;
    void add_parent(int a);
private:
    bool Aid;
    vector<int> parents;
};

void Stop::add_parent(int a){
    parents.push_back(a);
}

#ifndef MAX
#define MAX 100000
#endif

int main(int argc, char const *argv[]) {
    int N, M, K;
    Stop stop[MAX];
    stop[1].next=0;
    stop[1].able=1;
    cout << "Enter stuff: "; // <---
    
    cin>>N>>M>>K;
    cout<<"N: " << N<<endl; // <---
    return 0;
}
Last edited on
@kemort:
Nothing change. It doesn't even show
Enter stuff: 

closed account (48T7M4Gy)
It shows the prompt and result on my machine. Have you got the monitor turned on?

Depending on your IDE and the way you have set it up - codeblocks, VS, Xcode etc - the reason you now have a blank screen is possibly also because you need to run the exe file in the console.
Last edited on
closed account (48T7M4Gy)
You can also run it here using the cpp shell by pressing the wheel in the top RH corner of the post.

Enter stuff: 23 3 6
N: 23
Last edited on
I see. But when I try to run the program with CMD and ConEmu. It crashed.
Message:
program.exe has stopped working
ConEmu is (apparently, I had to look it up) a Windows terminal emulator. Why do you need to use it? Terminal emulators often don't run with all the native programs for their operating systems.

If you are running on a Windows system, just use the standard command line.
If you are on linux, use a normal shell.
If you are on an iPad ... don't bother.
I'm on a Windows system and used the standard command line. Same.
I will try to run on Linux later. Thanks for the advice!!
tomtran3110,

Which compiler are you using? Are you compiling within the same environment as you are trying to run the program? (For example, if you compiled within codeblocks or VisualStudio and then tried to run from outside these suites you may, by default, load different run-time libraries.)

As kemort pointed out, you can test (and edit, then test) your code in the shell which appears in the top RH corner for any code enclosed in code tags. Even when I turned on the most pedantic warnings and dropped to the oldest C++ standard it would compile and run your original code with and without any occurrence of parent_count.
Last edited on
Topic archived. No new replies allowed.