no appropriate default constructor available

I have an assignment where I have to take an array of numbers and systematically arrange it so that certain numbers will only move at certain times. I haven't practiced with classes all that much, so I hope its just a syntax error rather than something not actually possible. Thanks in advance.

Here is my header

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
#include <iomanip>
#include <iostream>
using namespace std;

class Traffic
{
	private:
		static const int size=9; 
		int traffic[size];
		bool look;
	public:
		Traffic (int[],int,bool);
		void Declaration (int[],int);
		void MoveR (int[],int);
		void JumpR (int[],int);
		void MoveL (int[],int);
		void JumpL (int[],int);
};

Traffic::Traffic (int jam[], int SIZE, bool it)
{
	for (int C=0; C<SIZE; C++)
    {
        cout << jam[C];
    }
    cout << endl;
	if (jam[3]==8&&jam[5]==1)
	{
		it=true;
	}
}

void Traffic::Declaration (int jam[], int SIZE)
{
	traffic[size]=jam[SIZE];
}
void Traffic::MoveR (int bunch[],int spot)
//switches so Moves right
{
	int temp = bunch[spot];
	bunch[spot] = bunch[spot+1];
	bunch[spot+1] = temp;
}
void Traffic::JumpR (int bunch[],int spot)
//switches so Jumps right
{
	int temp = bunch[spot];
	bunch[spot] = bunch[spot+2];
	bunch[spot+2] = temp;
}

void Traffic::MoveL (int bunch[],int spot)
//switches so Move left
{
	int temp = bunch[spot];
	bunch[spot] = bunch[spot-1];
	bunch[spot-1] = temp;
}
void Traffic::JumpL (int bunch[],int spot)
//switches so Jumps left
{
	int temp = bunch[spot];
	bunch[spot] = bunch[spot-2];
	bunch[spot-2] = temp;
}
And here is my source file

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "Tails.h"
void DanceRight (int[],int);
void DanceLeft (int [], int);
int main ()
{
	Traffic Slow;
    const int SIZE=9; // Size of the Array
    int Pro[SIZE]={1,2,3,4,0,5,6,7,8}; // Starts out the array
	bool swap; // End checker
	swap=false; 
	
	Slow.Declaration(Pro,SIZE);
	do 
	{
		DanceRight (Pro,SIZE); //checks if any values can be moved right
		DanceLeft (Pro,SIZE); // //checks if any values can be moved left		
	}
	while (!swap); // does while swap isnt true
       
    system("PAUSE");
}



void DanceLeft (int jam[], int SIZE)    // sees if values can move left
{
	Traffic Slow;
	for (int thing=1; thing<SIZE; thing++)
    {
		if (jam[thing]>4) 
		// checks to see if value can move right
		{
			if (jam[thing-2]<5&&jam[thing-1]==0)
		// if moving causes the puzzle to get trapped, ends the switches to the other side 
			{
				break;
			}
			else if (jam[thing-1]==0)
		//Moves Right
			{
				Slow.MoveL (jam,thing);
			}
			else if (jam[thing-1]<5&&jam[thing-2]==0)
		//Jumps Right
			{
				Slow.JumpL (jam,thing);
			}
			else 
		//Any other outcome means I did something wrong
			{
				cout << "Try again";
				system("PAUSE");
			}
		}
	}
}
void DanceRight (int jam[], int SIZE)  // sees if values can move Right
{
	Traffic Slow;
	for (int thing=(SIZE-2); thing>0; thing--)
    {
		if (jam[thing]<5&&jam[thing]>0) 
		// checks to see if value can move right
		{
			if (jam[thing+2]<5&&jam[thing+1]==0)
		// if moving causes the puzzle to get trapped, ends the switches to the other side 
			{
				break;
			}
			else if (jam[thing+1]==0)
		//Moves Right
			{
				Slow.MoveR (jam,thing);
			}
			else if (jam[thing+1]>4&&jam[thing+2]==0)
		//Jumps Right
			{
				Slow.JumpR (jam,thing);
			}
			else 
		//Any other outcome means I did something wrong
			{
				cout << "Try again";
				system("PAUSE");
			}
		}
	}
}


It told me that there was not appropriate default constructor available on lines 6, 27, and 57. Since then I tried changing the constructor so that it called no variables and only altered the private class members, but then it told me that there was an overloaded function in addition to the earlier errors.
Last edited on
Code tags, line, exact error message.
closed account (o3hC5Di1)
Hi there,

Could you do us a favour and wrap that code in code tags?
Just click on the <> button on the right of the input box.

It will help to make your code more readable.
If you can also apply some indentation, but tabs can be an issue so that doesn't always work when copying.

Also, could you describe what the problem is, is it compiler errors or unexpected behaviour?
If the former, which errors, if the latter which behaviour do you expect?

Thanks!

All the best,
NwN
Sorry about that NwN. fixed now.
The problem is exactly what the error message says.
You're trying to instantiate a Traffic object without any arguments, however Traffic has only one constructor, which has three parameters - not zero.
closed account (o3hC5Di1)
Hi there,

Thanks for that.

The solution is actually pretty simple.
You've provided an overloaded constructor, taking an int[] and bool as arguments.
When you do that, you also need to specify a default constructor yourself, C++ will not use it's own standard one any more.

You can just do following in the class declaration: Traffic() {};

Do keep in mind that when do so, and you create an object like so: Traffic Slow; (as you do), it will not set any variables in the class, which can lead to unexpected behaviour. In order to use your own overloaded constrcutor you need to do Traffic Slow = Traffic(intvariable[], boolvariable);

Hope that helps.

All the best,
NwN
Last edited on
Thanks for that. Well after some editing.

1
2
3
4
5
6
7
8
9
10
11
12
Traffic::Traffic ()
{
    for (int C=0; C<size; C++)
    {
        cout << traffic[C];
    }
    cout << endl;
    if (traffic[3]==8&&traffic[5]==1)
    {
        look=true;
    }
}


and changed the class declaration as well, and now it runs, but the display is hard to figure out. I lots of negative eight digit numbers and I have no idea where I messed up.
Last edited on
You never filled the traffic array with any values, so all of its elements are unintialized and contain arbitrary values. Why are you surprised about that? What did you expect it to print?
I filled the pro array with values and set the traffic array equal to the pro array in the function named Declaration. How should I have done that?
a constructor runs as soon as it is created, you don't assign any values to the array until after it is created, so traffic[x] is uninitialized.
Ah. Sorry about that. I tried this now:

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
#include <iomanip>
#include <iostream>
using namespace std;

class Traffic
{
    private:
        static const int size=9;
        int traffic[size];
        bool look;
    public:
        Traffic ();
		Traffic (int[],int,bool);
        void Declaration (int[],int);
        void MoveR (int[],int);
        void JumpR (int[],int);
        void MoveL (int[],int);
        void JumpL (int[],int);
};

Traffic::Traffic (jam[],SIZE,it)
{
	traffic[size]=jam[SIZE];
	look=it;
    for (int C=0; C<size; C++)
    {
        cout << traffic[C];
    }
    cout << endl;
    if (traffic[3]==8&&traffic[5]==1)
    {
        look=true;
    }
}

void Traffic::Declaration (int jam[], int SIZE)
{
    traffic[size]=jam[SIZE];
}
void Traffic::MoveR (int bunch[],int spot)
//switches so Moves right
{
    int temp = bunch[spot];
    bunch[spot] = bunch[spot+1];
    bunch[spot+1] = temp;
}
void Traffic::JumpR (int bunch[],int spot)
//switches so Jumps right
{
    int temp = bunch[spot];
    bunch[spot] = bunch[spot+2];
    bunch[spot+2] = temp;
}

void Traffic::MoveL (int bunch[],int spot)
//switches so Move left
{
    int temp = bunch[spot];
    bunch[spot] = bunch[spot-1];
    bunch[spot-1] = temp;
}
void Traffic::JumpL (int bunch[],int spot)
//switches so Jumps left
{
    int temp = bunch[spot];
    bunch[spot] = bunch[spot-2];
    bunch[spot-2] = temp;
}


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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "Tails.h"
void DanceRight (int[],int);
void DanceLeft (int [], int);
int main ()
{
    
    const int SIZE=9; // Size of the Array
    int Pro[SIZE]={1,2,3,4,0,5,6,7,8}; // Starts out the array
    bool swap; // End checker
    swap=false;
	Traffic Slow(Pro,SIZE,swap);
   
    Slow.Declaration(Pro,SIZE);
    do
    {
        DanceRight (Pro,SIZE); //checks if any values can be moved right
        DanceLeft (Pro,SIZE); // //checks if any values can be moved left       
    }
    while (!swap); // does while swap isnt true
      
    system("PAUSE");
}



void DanceLeft (int jam[], int SIZE)    // sees if values can move left
{
    Traffic Slow;
    for (int thing=1; thing<SIZE; thing++)
    {
        if (jam[thing]>4)
        // checks to see if value can move right
        {
            if (jam[thing-2]<5&&jam[thing-1]==0)
        // if moving causes the puzzle to get trapped, ends the switches to the other side
            {
                break;
            }
            else if (jam[thing-1]==0)
        //Moves Right
            {
                Slow.MoveL (jam,thing);
            }
            else if (jam[thing-1]<5&&jam[thing-2]==0)
        //Jumps Right
            {
                Slow.JumpL (jam,thing);
            }
            else
        //Any other outcome means I did something wrong
            {
                cout << "Try again";
                system("PAUSE");
            }
        }
    }
}
void DanceRight (int jam[], int SIZE)  // sees if values can move Right
{
    Traffic Slow;
    for (int thing=(SIZE-2); thing>0; thing--)
    {
        if (jam[thing]<5&&jam[thing]>0)
        // checks to see if value can move right
        {
            if (jam[thing+2]<5&&jam[thing+1]==0)
        // if moving causes the puzzle to get trapped, ends the switches to the other side
            {
                break;
            }
            else if (jam[thing+1]==0)
        //Moves Right
            {
                Slow.MoveR (jam,thing);
            }
            else if (jam[thing+1]>4&&jam[thing+2]==0)
        //Jumps Right
            {
                Slow.JumpR (jam,thing);
            }
            else
        //Any other outcome means I did something wrong
            {
                cout << "Try again";
                system("PAUSE");
            }
        }
    }


And now I have several problems with line 21 of the header. Thanks in advance and you've all been great help so far.

1>c:\users\lib05p\documents\visual studio 2008\projects\traffic\traffic\tails.h(22) : error C2065: 'jam' : undeclared identifier
1>c:\users\lib05p\documents\visual studio 2008\projects\traffic\traffic\tails.h(22) : error C2059: syntax error : ']'
1>c:\users\lib05p\documents\visual studio 2008\projects\traffic\traffic\tails.h(23) : error C2143: syntax error : missing ';' before '{'
1>c:\users\lib05p\documents\visual studio 2008\projects\traffic\traffic\tails.h(23) : error C2447: '{' : missing function header (old-style formal list?)

The types in line 21 are missing.
Also, for lines like these:
traffic[size]=jam[SIZE];
If size and SIZE are the respective array sizes, then both are out-of-bound accesses.
Topic archived. No new replies allowed.