开发者

static members and consts [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.
class a
{
protected:
  const int _ID;

public:
  a::a(int id){};
  a::top(int num);
};

class b : public a
{
  static int ok;
  b::b(int id):a(id){};
  a::top(ok);
}

int main()
{
  int t=5;
  b opj=b(t);
}

first why i get this compile error that solved only when i remove the const

non-static const member ‘const int Student::_ID’, can't use default assignment开发者_StackOverflow社区 operator - instantiated from ‘void std::vector::_M_insert_aux(__gnu_cxx::__normal_iterator, const _Tp&) [with _Tp = Student, _Alloc = std::allocator]’

second

i have anther problem

undefined reference to b::ok


Second first: b::ok has been declared, but not defined. Someplace, (preferably b.cpp), you need to add:

  int b::ok;

As for your first problem, _ID is const, it value cannot be changed -- but, you never give it a value to start with. You have to assign it an initial value:

  protected:
      const int _ID = 1234;

Now, you really should defined _ID as well, as we did with b::ok, but since it is const, the compiler may let you get away without doing that (some conditions apply).


This code has too may reasons to report compile errors.

  • In C++ you are not allowed to use qualified names when declaring class methods, so your a::a and a::top in the definition of class a are flat-out illegal.
  • What method declarations without a return type are supposed to mean is not clear either.
  • A declaration of a::top in b makes no sense either. What is it doing there? And what was the intent of having a::top(ok) in the middle of the class definition?

In other words, you code makes no sense whatsoever. It is not even a remotely C++ code. If you feed this into a C++ compiler the results will be pretty much as meaningful as if you fed Shakespeare's "Hamlet" into a C++ compiler. It is not possible to say what's wrong with the code, when everything is wrong. If this is some other language, please, tag it accordingly instead of [C++].


Since _ID is const, it has to be initialized in the constructor/base member initialization list. Sort of like this:

a::a(int id) : _ID(5) {};

However, I got a ton of errors when I tried to build this. Are you sure you pasted the whole thing?


To answer your second question first: ok is a static member and needs to be initialized as such. Include a line similar to:

int b::ok = 0;

...in the implementation file for your class b.

As for the _ID member: You declared _ID to be a constant, but you did not define it, i.e. you did not provide a value for the constant. Since it is constant and you are not allowed to change it later - how do you expect _ID to take a value then?

Also, there are some other "funny" things going on in your source code that make me advise you to (re-)read an introductory text.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜