1

I want to display the objects of the class and the number of objects by using a static function. I typed this code but it does not work. It gives an error Too many types indeclaration" and "undefined symbol getCount. Can anyone help me? where is actually error in this code?

#include<iostream>
#include<string>

class Bag {
private:
    static int objectCount;
    int Weight;
    std::string Brand;
    std::string Type;
    std::string Material;
    std::string Colour;
public:
    Bag(int W, std::string B, std::string T, std::string M, std::string C) {
        Weight = W;
        Brand = B;
        Type = T;
        Material = M;
        Colour = C;
        objectCount++;
    }

    void print() {
        std::cout << "\n";
        std::cout << "Bag: \n\n";
        std::cout << "Weight:\t\t" << Weight   << "kg" << '\n';
        std::cout << "Brand:\t\t"  << Brand    << '\n' << "Type:\t\t"   << Type   << '\n';
        std::cout << "Material:\t" << Material << '\n' << "colour:\t\t" << Colour << std::endl;
    }

    static int getCount() {
        return objectCount;
    }
};

int Bag::objectCount = 0;

int main() {
    Bag bag_1(2, "Slazanger", "Atheletic Bag", "Polyethylene", "Brown");
    bag_1.print();
    std::cout << "object count " << Bag::getCount() << '\n';

    Bag bag_2(4, "Samsonite", "Travel Bag", "Synthetic Fibre", "Gray");
    bag_2.print();
    std::cout << "object count " << Bag::getCount() << '\n';

    Bag bag_3(5, "Herschel", "Duffel bag", "Leather", "Black");
    bag_3.print();
    std::cout << "object count " << Bag::getCount() << '\n';

    Bag bag_4(3, "Kewin Woods", "Hand Bag", "Fibre", "Blue");
    bag_4.print();
    std::cout << "object count " << Bag::getCount() << std::endl;

    while(!std::cin.get());
    return 0;
}
Casey
  • 10,297
  • 11
  • 59
  • 88
  • what language is this supposed to be in? – niklasfi Apr 20 '14 at 18:14
  • 1
    I think you are missing a closing `}` please make sure to properly format your code, so that these kinds of problems show up. – niklasfi Apr 20 '14 at 18:17
  • It's an OPP using Borland(complier) C++. – user3351862 Apr 20 '14 at 18:18
  • You and others will have a much easier time to spot the error if the code is properly formatted. – Janick Bernet Apr 20 '14 at 18:26
  • I think its a properly formatted, why you don't think so?Also its not a too long code to spot the error. – user3351862 Apr 20 '14 at 18:34
  • I've properly formatted your code for you. I've also fixed it. It works as expected on an MSVC compiler. Your problem was that the `getCount` method was not inside the scope of the class and that constructing all your bags at once and *then* calling `getCount` 5 times will only net you the number 5 repeatedly. You must inspect the count variable between each construction of each object. – Casey Jun 22 '14 at 15:53

2 Answers2

1

You are scoping it incorrectly, getCount is statically scoped to the translation unit, not the class. Thus it has no symbol named objectCount available to it.

To fix it, merely put the method inside the class.

class Bag {
private:
    static int objectCount;
    int Weight;
    string Brand,Type,Material,Colour;
public:
    Bag(int W ,string B ,string T,string M,string C)
    {
        Weight=W;
        Brand=B;
        Type=T;
        Material=M;
        Colour=C;

        objectCount++;
    }

    void print()
    {
        cout<<"\n";
        cout<<"Bag: \n\n";
        cout<<"Weight:\t\t"<<Weight<<"kg"<<endl;
        cout<<"Brand:\t\t"<<Brand<<endl<<"Type:\t\t"<<Type<<endl;
        cout<<"Material:\t"<<Material<<endl<<"colour:\t\t"<<Colour<<endl;
    }

   static int getCount()
   {
       cout<< objectCount;
   }
};

Aditionally, Borland is a really old compiler and suprised to even still hear it's name, last release was around 15 years ago so you should really consider using clang, gcc or msvc and upgrading your learning materials to something less ancient. There has been alot of evolution in terms of practices, standards and compiler conformance.

For example, C++ headers don't have an extension, and other small things like that.

Casper Beyer
  • 2,203
  • 2
  • 22
  • 35
0

This is a working version of your code:

#include <iostream>
#include <cstring>

using namespace std;

class Bag {
private:
  static int objectCount;
  int Weight;
  string Brand, Type, Material, Colour;

public:
  Bag(int W, string B, string T, string M, string C) //constructor
  {
    Weight = W;
    Brand = B;
    Type = T;
    Material = M;
    Colour = C;

    objectCount++;
  }
  void print() {
    cout << "\n";
    cout << "Bag: \n\n";
    cout << "Weight:\t\t" << Weight << "kg" << endl;
    cout << "Brand:\t\t" << Brand << endl << "Type:\t\t" << Type << endl;
    cout << "Material:\t" << Material << endl << "colour:\t\t" << Colour
         << endl;
  }
  static int getCount() //static function to count objects
  {
    cout << objectCount;
  };
};


int Bag::objectCount = 0;
int main() {

  Bag bag_1(2, "Slazanger", "Atheletic Bag", "Polyethylene", "Brown");

  Bag bag_2(4, "Samsonite", "Travel Bag", "Synthetic Fibre", "Gray");
  Bag bag_3(5, "Herschel", "Duffel bag", "Leather", "Black");
  Bag bag_4(3, "Kewin Woods", "Hand Bag", "Fibre", "Blue");
  bag_1.print();
  cout << "object count" << Bag::getCount();
  bag_2.print();
  cout << "object count" << Bag::getCount();

  bag_3.print();
  cout << "object count" << Bag::getCount();

  bag_4.print();
  cout << "object count" << Bag::getCount();
}

There were several mistakes in the version you posted:

  • in C++ you don't need the .h when including files
  • you were using cout without the std:: qualifier or adding using namespace std; to your source. Also, please read this.
  • your static function was not declared/defined inside your class definition
  • it should be int main instead of void main

One last note: I removed your #include <conio.h> which should probably read #include <conio> and getch because I compiled this on a linux machine. Feel free to add them back in if you want them.

Community
  • 1
  • 1
niklasfi
  • 15,245
  • 7
  • 40
  • 54
  • I'm not sure if this was true in C++84 or not (which is what OP was programming in most likely) but using the constructor initializer list is much easier than default initialization then assignment. This isn't advice for you, because I'm sure you know it, just so that OP can understand. –  Apr 20 '14 at 18:52
  • There is no `cconio` as its a microsoft specific utility, so it is just conio.h`. Only headers from the C standard libraries are pulled in as extensionless files with a 'c' prefix. – Casper Beyer Apr 20 '14 at 20:29