Questions about initializing c++ member variables

Asked 2 years ago, Updated 2 years ago, 107 views

#include "stdafx.h"
#include <iostream>
using namespace std;

class foam
{
public:
    int temp_c ;

    foam() 
    { 
     temp_a = 10;
     temp_b = 20;
    }

    int temp_a ;
    int temp_b ;



    void printdata()
    {
        cout << "temp_a : " << temp_a << endl;
        cout << "temp_b : " << temp_b << endl;
    }


};

int _tmain(int argc, _TCHAR* argv[])
{    
    foam exam = { 30 };
    exam.printdata();

    return 0;
}

What I meant was that temp_a and temp_b were automatically initialized by the constructor in the foam class temp_c was intended to initialize in the tmain function.

foam example = {30};

in
"error C2552: 'exam' : Cannot initialize non-aggregation using the initializer list. "

The error occurs.

As I intended, some of the class member variables in c++11 grammar are initialized internally What are some variables that are initialized in the _main function?

c++ c++11

2022-09-21 12:15

1 Answers

Blockquote

Do it like below

Generator

foam(int val) 
{ 
    temp_a = 10;
    temp_b = 20;
    temp_c = val;
}

class declaration

foam exam(30);


2022-09-21 12:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.