【问题标题】:object oriented programming,use of static function to count objects面向对象编程,使用静态函数计算对象
【发布时间】:2014-04-20 18:11:23
【问题描述】:

我想用一个静态函数来显示类的对象和对象的数量。我输入了这段代码,但它不起作用。它给出了一个错误Too many types indeclaration" and "undefined symbol getCount。谁能帮我?这段代码的实际错误在哪里?

#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;
}

【问题讨论】:

  • 这应该是什么语言?
  • 我认为您错过了结束} 请确保正确格式化您的代码,以便出现此类问题。
  • 这是一个使用 Borland(complier) C++ 的 OPP。
  • 如果代码格式正确,您和其他人将更容易发现错误。
  • 我认为它的格式正确,为什么你不这么认为?而且它的代码不会太长而无法发现错误。

标签: c++ class oop static-members


【解决方案1】:

您的范围不正确,getCount 是静态范围内的翻译 单位,而不是班级。因此它没有可用的名为objectCount 的符号。

要修复它,只需将方法放在类中即可。

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;
   }
};

此外,Borland 是一个非常古老的编译器,甚至仍然令人惊讶 听说它的名字,上一次发布是在 15 年前,所以你真的应该 考虑使用clanggccmsvc 并将您的学习材料升级到 不那么古老的东西。在实践方面已经有了很大的发展, 标准和编译器一致性。

例如,C++ 头文件没有扩展名,以及其他类似的小东西。

【讨论】:

    【解决方案2】:

    这是您的代码的工作版本:

    #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();
    }
    

    您发布的版本有几个错误:

    • 在 C++ 中,包含文件时不需要 .h
    • 您使用的cout 没有std:: 限定符或将using namespace std; 添加到您的源。另外,请阅读this
    • 您的静态函数未在您的类定义中声明/定义
    • 应该是int main 而不是void main

    最后一点:我删除了您的#include &lt;conio.h&gt;,它可能应该是#include &lt;conio&gt;getch,因为我是在Linux 机器上编译的。如果需要,请随时将它们重新添加。

    【讨论】:

    • 我不确定这在 C++84 中是否正确(这很可能是 OP 编程的内容),但使用构造函数初始化列表比默认初始化然后赋值要容易得多。这不是给你的建议,因为我确定你知道,只是为了让 OP 能够理解。
    • 没有 cconio 作为微软特定的实用程序,所以它只是 conio.h`。只有来自 C 标准库的头文件作为带有“c”前缀的无扩展名文件被拉入。
    猜你喜欢
    • 2011-11-16
    • 2015-03-20
    • 1970-01-01
    • 2023-03-21
    • 2018-06-23
    • 2010-11-04
    • 2018-08-02
    • 2012-09-09
    • 2014-02-23
    相关资源
    最近更新 更多