i am beginner in programming, start C++ one weak ago. i have problem to use of my static variable. i read about use of static variable in various same question but i understand just this Car::countOfInput;. from below post:
this is my code:
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
class Car{
private:
static int countOfInput;
char *carName;
double carNumber;
public:
Car() {
static int countOfInput = 0;
char carName = {'X'};
double carNumber = 0;
}
void setVal(){
double number;
cout << "Car Name: ";
char* str = new char[strlen(str) + 1];
cin>>str;
strcpy(carName, str);
cout << endl << "Car Number: ";
cin >> number; cout << endl;
carNumber = number;
Car::countOfInput += 1;
}
friend void print(){
if(Car::countOfInput == 0){
cout << "Error: empty!";
return;
}
cout << "LIST OF CarS" << endl;
cout << "Car Name: " << carName << "\t";
cout << "Car Number: " << carNumber << endl;
} const
void setCarNumber(int x){carNumber = x;}
int getCarNumber(){return carNumber;}
void setcarName(char x[]){strcpy(carName, x);}
char getcarName(){return *carName;}
int getCountOfInput(){return countOfInput;}
void setCountOfInput(int x){countOfInput = x;}
};
int main(){
Car product[3];
product[0].setVal();
product[0].print();
getch();
return 0;
}
when i run this:
F:\CLion\practise\main.cpp: In function 'void print()':
F:\CLion\practise\main.cpp:10:13: error: invalid use of non-static data member 'Car::carName' char *carName; ^
F:\CLion\practise\main.cpp:40:33: error: from this location cout << "Car Name: " << carName << "\t"; ^
F:\CLion\practise\main.cpp:11:12: error: invalid use of non-static data member 'Car::carNumber' double carNumber; ^
F:\CLion\practise\main.cpp:41:35: error: from this location cout << "Car Number: " << carNumber << endl; ^
F:\CLion\practise\main.cpp: In function 'int main()':
F:\CLion\practise\main.cpp:57:16: error: 'class Car' has no member named 'print' product[0].print();
I use CLion, Thanks in advance.