Why is this not working?

I don't know why my code doesn't work and my reference code works.



I am trying to copy this code by making my own code which is now directly similar to this code:
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
#include <iostream>
#include <string.h>

class Student
{
    private:
        std::string studentName;
        int studentAge;

    public:
        Student(std::string newName ="John Doe", int newAge=18)
        {
            studentName = std::move(newName);
            studentAge = newAge;
            std::cout << "Constructor Called." << std::endl;
        }
        ~Student()
        {
            std::cout << "Destructor Called." << std::endl;
        }
        Student(const Student &copyStudent)
        {
            std::cout << "Copy Constructor Called" << std::endl;
            studentName = copyStudent.studentName;
            studentAge = copyStudent.studentAge;
        }
        void printDetails()
        {
            std::cout << this->studentName << " " << this->studentAge << std::endl;
        }
};


int main()
{
    const size_t j = 5;

    Student studentList[j] = {};

    std::string namesList[j] = {"Carly", "Freddy", "Sam", "Zack", "Cody"};

    int ageList[j] = {15, 16, 18, 19, 16};

    return 0;
}


This is the code that I did which is now very similar to the one above

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
#include <iostream>
#include <string.h>

class Test
{
    private:
        std::string poop;
        int big_poop;

    public:
        Test(std::string i, int x)
        {
            poop=std::move(i);
            big_poop=x;
            std::cout<<"\nConstructor\n";
        }
        ~Test()
        {
            std::cout<<"\nDestructor\n";
        }
        Test(const Test &copyTest)
        {
            std::cout << "Copy Constructor Called" << std::endl;
            poop= copyTest.poop;
            big_poop= copyTest.big_poop;
        }
        void displayInfo()
        {
            std::cout << this->poop << " " << this->big_poop << std::endl;
        }
};


int main()
{
    const size_t j = 5;

    Test TestList[j] = {};

    std::string listofi[j]={"a","b","c","d","e"};

    int listofx [j]={1,2,3,4,5};

    return 0;
}



and I still have no idea on why I'm having this error:

error: could not convert '<brace-enclosed initializer list>()' from '<brace-enclosed initializer list>' to 'Test'|


any help would be appreciated

On line 38, you are trying to create an (array of) objects of type Test using a constructor with no arguments. However, the only constructors you provide are one taking two arguments and the copy constructor (not relevant here).

Either create a constructor taking no arguments, or put default values in (as in the first code). Say
Test(std::string i="", int x=0)
Oh my goodness, thank you so much.
Hello. In your previous code, you have some "default values" according to a string and an integer (line 11). Take a look at the constructor. In you own code, you forgot them (line 11 again) - so you cannot instantiate your constructor which waits some arguments. Set another constructor or set its arguments ++

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
#include <iostream>
#include <string.h>

class Test
{
public:
    Test(std::string i = "a", int x = 7)
    {
        poop = std::move(i);
        big_poop = x;
        std::cout << "\nConstructor\n";
    }

    ~Test()
    {
        std::cout << "\nDestructor\n";
    }

    Test(const Test& copyTest)
    {
        std::cout << "Copy Constructor Called" << std::endl;
        poop = copyTest.poop;
        big_poop = copyTest.big_poop;
    }

    void displayInfo()
    {
        std::cout << this->poop << " " << this->big_poop << std::endl;
    }

private:
    std::string poop;
    int big_poop;
};


int main()
{
    const size_t j = 5;
    Test TestList[j] = {}; // empty instantiation !!!
    //std::string listofi[j] = { "a","b","c","d","e" };
    //int listofx[j] = { 1,2,3,4,5 };

    return 0;
}


But at this stage, all your arrays and some parts of code are just useles. What are you trying to do?
Last edited on
I guess that you are trying to use and instantiate a class - a first exercise. Maybe this code could be useful. It create five instantiations of a class and clean up memory. The constructor waits its arguments and I give them in the loop ++

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
#include <iostream>

class Test
{
public:
    Test(std::string s, int x) : poop(std::move(s)), big_poop(x)
    {  
        std::cout << "Constructor " << poop << " " << big_poop << std::endl;
    }

    ~Test()
    {
        std::cout << "Destructor" << std::endl;
    }


private:
    std::string poop;
    int big_poop;
};

int main()
{
    const size_t j = 5;
    std::string listofi[j] = { "a", "b", "c", "d", "e" };
    int listofx[j] = { 1, 2, 3, 4, 5 };

    for (int i = 0; i < j; i++)
    {
        Test* testList = new Test(listofi[i], listofx[i]);
        delete testList;
    }

    return 0;
}



Constructor a 1
Destructor
Constructor b 2
Destructor
Constructor c 3
Destructor
Constructor d 4
Destructor
Constructor e 5
Destructor
Last edited on
Don't use string.h for the include - it's string. If you pass std::string by value then assign using move semantics to avoid a second copy.

Consider:

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
#include <iostream>
#include <string>
#include <vector>

class Test {
private:
	std::string poop;
	int big_poop {};

public:
	Test() = default;
	Test(std::string i, int x) : poop(std::move(i)), big_poop(x) {
		std::cout << "Constructor\n";
	}

	~Test() {
		std::cout << "Destructor\n";
	}

	Test(const Test& copyTest) : Test(copyTest.poop, copyTest.big_poop) {
		std::cout << "Copy Constructor Called\n";
	}

	Test(Test&& copyTest) noexcept : Test(std::move(copyTest.poop), copyTest.big_poop) {
		std::cout << "Move Constructor Called\n";
	}

	void displayInfo() const {
		std::cout << poop << ' ' << big_poop << '\n';
	}
};

int main() {
	const size_t j { 5 };

	const std::string listofi[j] { "a","b","c","d","e" };
	const int listofx[j] { 1,2,3,4,5 };
	std::vector<Test> TestList;
	TestList.reserve(j);

	for (size_t i {}; i < j; ++i)
		TestList.emplace_back(listofi[i], listofx[i]);

	for (const auto& t : TestList)
		t.displayInfo();
}


which displays:


Constructor
Constructor
Constructor
Constructor
Constructor
a 1
b 2
c 3
d 4
e 5
Destructor
Destructor
Destructor
Destructor
Destructor

Don't use string.h for the include - it's string.

Hey! Did not see that :/
Topic archived. No new replies allowed.