【发布时间】: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