小编Le *_*Anh的帖子

无法使用“派生”类型的表达式初始化“父”类型的对象参数

我正在实现一个简单的层次结构:系统和设备将是两个基类。系统具有设备。然后我有一个从系统派生的网络,它将有计算机。计算机源自系统和设备。最后,计算机有各种设备,如 CPU、内存等。计算机之所以是设备,是因为网络是一个系统,而系统存储设备。计算机是系统的原因是因为它存储设备(CPU、内存等)

无论如何,到目前为止我的实现:

class Device {
public:
    explicit Device(unsigned property): property(property) {}
    friend void swap(Device &first, Device &second) {
        using std::swap;
        swap(first.property, second.property);
    }
    Device& operator=(Device other) {
        swap(*this, other);
        return *this;
    }
private:
    Device() = default;

    unsigned property;
};

class System {
public:
    explicit System(const string &name): name(name) {}
    friend void swap(System &first, System &second) {
        using std::swap;
        swap(first.name, second.name);
        swap(first.devices, second.devices);
    }
    System& operator=(System other) {
        swap(*this, other);
        return *this;
    }
protected:
    void addDevice(Device b1) {
        devices.push_back(b1);
    } …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance

5
推荐指数
2
解决办法
1万
查看次数

为 RecyclerView 片段实现 ListAdapter

我已经使用普通的ViewAdapter成功完成了此操作,但我似乎无法使用ListAdapter来完成此操作。

这是我的片段,它完成了大部分工作:

class CrimeListFragment: Fragment() {

    //Required interface for hosting activities
    interface Callbacks {
        fun onCrimeSelected(crimeId: UUID)
    }

    private var callbacks: Callbacks? = null
    private lateinit var crimeRecyclerView: RecyclerView
    private val crimeListViewModel: CrimeListViewModel by lazy {
        ViewModelProviders.of(this).get(CrimeListViewModel::class.java)
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)

        callbacks = context as Callbacks?
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val view = inflater.inflate(R.layout.fragment_crime_list, container, false)

        crimeRecyclerView =
            view.findViewById(R.id.crime_recycler_view) as RecyclerView
        crimeRecyclerView.layoutManager …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-recyclerview android-livedata android-listadapter

5
推荐指数
1
解决办法
3439
查看次数

无法初始化 MockMaker

这是我的测试课:

class RocketListVMTest {

    @get:Rule
    var instantTaskExecutorRule = InstantTaskExecutorRule()

    private lateinit var sut: RocketListVM
    private var activeOnlyToggle = false

    private val repo: Repo = mock()

    @Before
    fun setUp() {
        sut = RocketListVM(repo)
        activeOnlyToggle = false
    }

    @Test
    fun toggleActiveOnlyWithTrueCallsRepository() {
        sut.toggleActiveOnly(true)

        verify(repo).getActiveOnlyLocalRockets()
    }
}
Run Code Online (Sandbox Code Playgroud)

具有以下依赖项:

androidTestImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito-inline:2.21.0'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'android.arch.core:core-testing:1.1.1'
Run Code Online (Sandbox Code Playgroud)

我已经src/androidTest/resources/mockito-extensions/org.mockito.plugins.MockMakermock-maker-inline里面创建了。

测试类失败,因为

java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker …
Run Code Online (Sandbox Code Playgroud)

android integration-testing android-testing mockito-kotlin

5
推荐指数
1
解决办法
2430
查看次数

free():实现向量的ease()时的无效指针

我正在尝试实现Vector类。首先,我的Vector尚不支持泛型,仅支持我的Thing类。

Vector类应支持:empty(),push_back(),erase()和下标运算符。如果需要,还应相应调整大小。

我的Vector实现:

class Vect {
public:
    Vect() {
        length = 0;
        capacity = 3;
        charCnt = 0;
        data = new Thing[capacity];
    }
    ~Vect() {
        delete[] data;
    }

    bool empty() const {
        return length == 0;
    }

    void push_back(const char *str) {
        if(length + 1 == capacity)
            doubleSize();
        data[length++] = Thing(str);
        charCnt += strlen(str);
    }

    bool erase(size_t at) {
        if(at >= length)
            return false;

        auto newData = new Thing[capacity];
        size_t newIndex = 0;

        for(size_t i = 0; i …
Run Code Online (Sandbox Code Playgroud)

c++ memory-management vector

2
推荐指数
1
解决办法
127
查看次数

重载+ =用于链操作

比方说我有:

class Date {
    int year, month, day;
}
Run Code Online (Sandbox Code Playgroud)

我有+运算符重载:

friend CDate operator +(const Date &leftDate, const Date &rightDate) {}
Run Code Online (Sandbox Code Playgroud)

我在正确的日期增加左边的日期.那部分似乎有效.

现在我想超载+=,如果我所做的一切都是微不足道的date += another_date.

但是,如果我不得不链它,比如:date += another_date + another_date_2我要创建一个向量,another_dateanother_date2会被保存,然后做加法为他们每个人依次是:

Date& Date::operator +=(const vector<Date> &dates) {
    for(auto date: dates) {
        this->year += date.year;
        this->month += date.month;
        this->day += date.day;
    }
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

我现在正在努力的部分是如何重载+运算符,它返回一个向量?

我的想法到目前为止:

  • vector<Date> operator +(const Date &date):我在哪里创建一个向量?我必须创建一个才能插入date.
  • vector<Date> operator +(vector<Date> &dates, …

c++ overloading vector

0
推荐指数
1
解决办法
61
查看次数