0

I have a class which has multiple data members of different data types. I was wondering if I can create and initialize an object using a constructor.

class test
{
     public:
            int a;
            string b;
            float c;

      test (int x, string y, float z);

};


int main()
{

    test t1(10,"Hello",2.5f); //this line doesn't work

}

kometen
  • 6,536
  • 6
  • 41
  • 51
Free-Man
  • 11
  • 1
  • _"I was wondering if I can create and initialize an object using a constructor."_ - that's basically the sole reason constructors exist ;) Please add some more details to your question to clarify _what exactly_ is not working. You only declared the constructor, but never defined it in the code shown. You also didn't include ``. – Lukas-T Jun 14 '21 at 11:54
  • 2
    Yes you can do that. Please show a more complete example and the exact error you are getting. – Cory Kramer Jun 14 '21 at 11:55
  • oh damn, my bad i completely forgot to define the constructor, i keep forgetting to define them and think that the values will automatically get assigned, i should have assigned the values inside the constructor, thanks @churill stupid mistake on my part – Free-Man Jun 14 '21 at 12:00

4 Answers4

1

You need to implement the constructor test (int x, std::string y, float z); Don't forget to #include <string> at the top of your source file.

Although you could use the notation

test t1{10, "Hello", 2.5f};

which will cause the arguments to be assigned to the class members in the order they are written; although you will have to drop the constructor completely.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

In your example you have declared the constructor, but never defined it.

class test
{
public:
    int a;
    std::string b;
    float c;

    test(int x, std::string y, float z) : a(x), b(y), c(z) {}
};


int main()
{
    test t1(10, "Hello", 2.5f);
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

The fact that it doesn't work is not really what we do on StackOverflow. You should provide an error message.

With that said, I suppose you are getting an error from the linker because your constructor does not include a definition, but only a declaration.

Here's a working example: https://godbolt.org/z/r89cf86s3

#include <string>

class test
{
public:
    int a;
    std::string b;
    float c;

    test (int x, std::string y, float z) : a{x}, b{y}, c{z}
    {}
};


int main()
{
    test t1(10, "Hello", 2.5f);
}

NOTE Passing std::string by value to the constructor can lead to temporary objects being created, so to keep the workings of this code and improve it, you could use a const-reference, i.e. const std::string& as parameter of the constructor.

Victor
  • 13,914
  • 19
  • 78
  • 147
-1

I forgot to define the constructor :(

it should've been like this:-

class test
{
     public:
            int a;
            string b;
            float c;

      test (int x, string y, float z)
    {
        a=x;
        b=y;
        c=z;

     }

};

int main() {

test t1(10,"Hello",2.5f); //this line doesn't work

}

kometen
  • 6,536
  • 6
  • 41
  • 51
Free-Man
  • 11
  • 1