inheritance

error: Invalid use of incomplete type

橙三吉。 提交于 2020-07-08 03:11:26
问题 I got the following problem, does anyone have a good idea? class Vector_2d; namespace Utils { class Align_vector : public Vector_2d { protected: bool check_range(int x, int y); public: enum alignment {left, right, up, down}; Align_vector(Alignment alignment); void set_alignment(Alignment alignment); Alignment get_alignment(); }; } the error is: error: invalid use of incomplete type ‘class Vector_2d’ But how is there an error? 回答1: class Vector_2d; This only declares a class by that name exits

error: Invalid use of incomplete type

拥有回忆 提交于 2020-07-08 03:11:23
问题 I got the following problem, does anyone have a good idea? class Vector_2d; namespace Utils { class Align_vector : public Vector_2d { protected: bool check_range(int x, int y); public: enum alignment {left, right, up, down}; Align_vector(Alignment alignment); void set_alignment(Alignment alignment); Alignment get_alignment(); }; } the error is: error: invalid use of incomplete type ‘class Vector_2d’ But how is there an error? 回答1: class Vector_2d; This only declares a class by that name exits

Why can't sub-packages see package private classes?

ⅰ亾dé卋堺 提交于 2020-07-06 08:34:11
问题 Okay so, I have this project structure: package A.B class SuperClass (this class is marked package private) package A.B.C class SubClass (inherits from super class) I'd rather not make SuperClass publicly visible... It is really just a utility class for this specific project (A.B). It seems to me that SubClass should be able to see SuperClass , because package A.B.C is a subpackage of A.B... but this is not the case. What would be the best way to resolve this issue? I don't think it makes

Operator + overloading in base class and using it in derived class

一笑奈何 提交于 2020-07-05 12:55:06
问题 I have 2 classes, base (with copy constructor) and derived, in base Ι have overloaded operator+ : class Base { public: Base(const Base& x) { // some code for copying } Base operator+(const Base &bignum) const { Base x; /* ... */ return x; } }; class Derived : public Base { }; And when i try to do something like that Derived x; Derived y; Derived c=x+y; I get error: conversion from "Base" to non-scalar type "Derived" derived Could problem be in that operator + returns object of Base type and I

Mutable class as a child of an immutable class

大城市里の小女人 提交于 2020-07-05 02:35:33
问题 I want to have immutable Java objects like this (strongly simplified): class Immutable { protected String name; public Immutable(String name) { this.name = name; } public String getName() { return name; } } In some cases the object should not only be readable but mutable, so I could add mutability through inheritance: public class Mutable extends Immutable { public Mutable(String name) { super(name); } public void setName(String name) { super.name = name; } } While this is technically fine, I

Can't access parent member variable in Python

让人想犯罪 __ 提交于 2020-07-04 07:25:22
问题 I'm trying to access a parent member variable from an extended class. But running the following code... class Mother(object): def __init__(self): self._haircolor = "Brown" class Child(Mother): def __init__(self): Mother.__init__(self) def print_haircolor(self): print Mother._haircolor c = Child() c.print_haircolor() Gets me this error: AttributeError: type object 'Mother' has no attribute '_haircolor' What am I doing wrong? 回答1: You're mixing up class and instance attributes. print self.

Destructing derived class by deleting base class

只愿长相守 提交于 2020-07-03 10:17:11
问题 I have a situation that looks like the following code: #include <iostream> class A { public: A() { std::cout << "A created!" << std::endl; } ~A() { std::cout << "A destroyed!" << std::endl; } virtual const char* Name() { return "A"; } }; class B : public A { public: B() { std::cout << "B created!" << std::endl; } ~B() { std::cout << "B destroyed!" << std::endl; } const char* Name() { return "B"; } }; int main() { A* a = new B(); std::cout << a->Name() << "\n"; delete a; return 0; } I want B

How to call base class method?

梦想与她 提交于 2020-07-03 05:01:50
问题 Say I have classes declared like this: public abstract class IdentifiableEntity { public boolean validate() { return true; } } public class PreferenceCategory extends IdentifiableEntity { public boolean validate() { return true; } } Now, let's say I have PreferenceCategory variable created, and I want to call the IdentifiableEntity.validate() method, not the PreferenceCategory.validate() method. I would have thought I could do this with a cast (see below), but it still calls the overridden

Java interface synthetic method generation while narrowing return type

≡放荡痞女 提交于 2020-06-27 16:39:08
问题 I have 2 interfaces, and 2 return types. interface interfaceA { Publisher<String> doSomething(); } interface interfaceB extends interfaceA { Flow<String> doSomething(); } interface Publisher<T>{} class Flow<T> implements Publisher<T>{} So at runtime, I can see 2 methods of interfaceB.class.getMethods() public default my.package.Publisher my.package.interfaceB.doSomething() public abstract my.package.Flow my.package.interfaceB.doSomething() Regarding the first one, it is Synthetic. (method

Why must we specify template arguments for template base class when calling its constructor?

风流意气都作罢 提交于 2020-06-27 09:03:50
问题 The following code snippet compiles only if I explicitly specify the template argument T for the Base struct in Derived ctor: template <class T> struct Base { Base(int) {} }; template <class T> struct Derived : Base<T> { Derived(int i) : Base<T>(i) {} }; If I call Base(i) instead of Base<T>(i) - it doesn't work. Why can't compiler determine that Base is actually Base<T> (because I derive from Base<T> )? Is this requirement made intentionally? 来源: https://stackoverflow.com/questions/52632748