1

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:

  1. How do I call a static method of another class

  2. C++ Static member method call on class instance

  3. How to call static method from another class?

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.

3
  • 1
    If a class variable is marked static, it isn't associated with any instance of its class. Are you sure that's what you want? Commented May 15, 2016 at 14:16
  • Roughly, i want increase count of my static variable every time setVal() function call. Commented May 15, 2016 at 14:18
  • 2
    I think you need to take much smaller steps. You have built up a mass of at least four separate errors here - writing less code and testing earlier would have simplified this. Commented May 15, 2016 at 14:33

3 Answers 3

4

The variable declaration inside Car() are local variables, not initializations of the member variables. To initialize the member variables, just do this (it's a member initializer list):

Car() : carName("X"), carNumber(0) {}

and put the definition of countOfInput outside the class in a .cpp file (in global scope):

int Car::countOfInput = 0;

(If you want to reset countOfInput to 0 every time the Car() constructor is called, you can do that in the constructor body: countOfInput = 0;)

Sign up to request clarification or add additional context in comments.

3 Comments

Ooooo, nice catch with locals. :) Seems like a quiz question from school.
What can i do if i want to have my print function be friend?
@DeathProgrammer Declare it friend inside the class and define it outside, like normally.
3

Oh dear. You have a number of problems. Firstly, your constructor doesn't initialize any of the member variables.

   Car() {
        static int countOfInput = 0;
        char carName = {'X'};
        double carNumber = 0;
    }

Instead it is declaring three local variables and setting them to values. What you want is:

   Car() 
       : carName(nullptr)
       , carNumber(0.0) 
   {
   }

Then setValue is copying the string into the memory pointed at by this->carName, but that is uninitialized, so could be anywhere. Also, strlen(str) before you initialize str is doomed to failure. Make carName a std::string, then you don't need to construct it, and the code becomes:

   Car() 
       : carNumber(0.0) 
   {
   }

   void setVal(){

        cout << "Car Name: ";
        cin >> carName;

        cout << endl << "Car Number: ";
        cin >> carNumber; cout << endl;

        Car::countOfInput += 1;
   }

Next, you need to make print not be a friend (and make it const).

   void print() const {
   ...

Finally you need to define coutOfInput. You have declared it, but you also need a definition. Outside any function do:

int Car::countOfInput = 0;

Comments

3

The error messages have nothing to do with static variables or static methods.

The keyword friend is in front of print(), make it a non-member function (note the last error message) and then can't access the member variables directly. According to the usage it should be a member function, so just remove the keyword friend.

And as @tuple_cat suggested, const should be put before the beginning {.

void print() const {
     if(Car::countOfInput == 0){
         cout << "Error: empty!";
         return;
     }
     cout << "LIST OF CarS" << endl;
     cout << "Car Name: " << carName << "\t";
     cout << "Car Number: " << carNumber << endl;
 }

4 Comments

And put const before the opening {.
without friend and even const and even put const before the beginninng, not working yet.
Give me this error: F:/CLion/practise/main.cpp:31: undefined reference to `Car::countOfInput'.
@DeathProgrammer It's another issue. See tuple_cat's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.