繁体   English   中英

从向量访问某些类函数C ++的问题

[英]Pr oblems Accessing Certain Class Functions C++ from Vector

我有一个类型为Tab的向量,但是当我在Tab的向量上调用类函数时,没有任何输出。 但是,我也没有收到任何错误。

如果您知道什么是Android启动器,则可以认为这是Android启动器的基于控制台的简单测试用例,带有分类的选项卡,可让您滚动到另一个选项卡。

main.cpp

#include <iostream>
#include <vector>
#include <string>
#include "Tab.h"

void addVectOfTabs(std::vector<Tab>& tabs);
void swipe(std::vector<Tab>& tabs);
int main()
{
    std::vector<Tab> tabs;
    tabs.reserve(3);
    addVectOfTabs(tabs);
    swipe(tabs);
    return 0;
}

void addVectOfTabs(std::vector<Tab>& tabs)//adds values to the vector(arrayList) of tabs
{
    Tab google  ("Google", "0001", "#ffffff");
    Tab xPosed  ("XPosed", "0002", "#aaaaaa");
    Tab custom  ("Custom", "0003", "#dddddd");
    tabs.push_back(google);//don't have to use new keyword for whatever reason (C++ is weird)
    tabs.push_back(xPosed);
    tabs.push_back(custom);//just some bs values for these
    std::cout<<google.getTabColor(); //this works
}

void swipe(std::vector<Tab>& tabs)//automated simulated swiping
{
    for(int i=0;i<tabs.size();i++)
    {
        int tabSize = tabs[i].getAppsInDrawerSize();
        tabs[i].showName();
        std::cout << "\n\n\n" << "**********************" << tabs[i].getTabID() << "*************";
        std::cout << "\n" << tabSize;
        for(int j=0;j < tabSize;j++)
        {
            std::cout<<"\nYou are at app " << (j+1) << " of " << tabSize;
        }
        if (i>tabSize)//I wrote this code late last night and have no clue what I was thinking with this ine
            i++;
    }
}

表h

#ifndef TAB_H
#define TAB_H
#include <vector>
#include <string>
#include <array>


class Tab
{
    public:
        Tab();
        virtual ~Tab();
        Tab(const Tab& other);
        Tab(std::string tabName, std::string tabID, std::string tabColor);
        std::array<int,10>& getAppsInDrawer();
        std::string getTabName();
        std::string& getTabID();
        std::string& getTabColor();
        int getAppsInDrawerSize();
        void showName(); //can't access
    protected:

    private:
        std::array<int, 10> appsInDrawer; //vector is like arraylist in Java; simulation of apps in the app drawer
        void populateAppsInDrawer();
        std::string tabName;
        std::string tabID;
        std::string tabColor;
};

#endif // TAB_H

Tab.cpp

#include <string>
#include <iostream>
#include "Tab.h"


Tab::Tab()
{
    //ctor
}

Tab::~Tab()
{
    //dtor
}

Tab::Tab(const Tab& other)
{
    //copy ctor
}

Tab::Tab(std::string tabN, std::string tabId, std::string tabClor): tabName(tabN), tabID(tabId), tabColor(tabClor)
{

}

std::array<int,10>& Tab::getAppsInDrawer()
{
    return appsInDrawer;
}

std::string Tab::getTabName()
{
    return tabName;
}

std::string& Tab::getTabID()
{
    return tabID;
}

std::string& Tab::getTabColor()
{
    return tabColor;
}

void Tab::populateAppsInDrawer()//for simulation purposes
{
    for(int i=0;i<10;i++)
    {
        appsInDrawer[i]=i+1;//adds an item; similar to arrayList.add()
    }
}

int Tab::getAppsInDrawerSize()
{
    return appsInDrawer.size();
}

void Tab::showName()
{
    std::cout << tabName;
}

我知道在我的代码中我返回了一些地址和一些字符串,但这只是为了测试一种方法是否可以在另一种方法上工作而已,但事实并非如此。

只是添加到我上面的评论中。 问题是您的Tab类已经声明了一个复制构造函数,但在构造函数的主体中没有任何内容,因此这就是为什么将对象放置在向量中时它们似乎无能为力的原因。 如果可以访问与c ++ 11兼容的编译器,则可以执行以下操作:

#include <utility>
...
tabs.push_back(std::move(google));

在tabs类中,添加以下构造函数:

explicit Tab(Tab &&other) = default;

这比复制更有效,因为它将把您当前拥有的对象放入向量中,而不是复制它,从而创建同一对象的2个副本

例如: http//rextester.com/MNB8052

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM