0

I have a vector of type Tab, but when I call class functions on the vector of Tabs I get no output. However, I don't receive any errors either.

If you know what an Android launcher is, then this is supposed to be a simple console-based testcase of an Android launcher with categorized tabs that allow you to scroll to another tab.

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++;
    }
}

Tab.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;
}

I know in my code I return some addresses and some strings, but that was just to test to see if one method would work over the other, but it doesn't.

Andrue
  • 688
  • 3
  • 11
  • 27
  • 2
    You need to actually implement your copy constructor `Tab::Tab(const Tab& other)`, or just delete your emtpy implementation and let the compiler do the work, since it seems to mostly consist of copyable types – PeterT Oct 07 '14 at 15:14
  • 2
    I have no idea what you expect the code to do or what it's actually doing. I'm not going to bother copying and pasting all your code into three separate files just to find out. Please clarify your question. – Jonathan Wakely Oct 07 '14 at 15:14
  • Just so you know `std::array` is not resizable, so if that is what you plan to do with it in the future, you are better off using `std::vector` which is very much like `java.util.ArrayList`. Also like somebody already mentioned, you need to implement a copy constructor because in C++ unlike Java, objects are copied rather than referenced – smac89 Oct 07 '14 at 15:16
  • @Smac89 I realize that, but I didn't intend for it to be resized. I've always wondered what a copy constructor was for, but I've never had a need to use one until now. Thanks. – Andrue Oct 07 '14 at 16:17

1 Answers1

0

Just adding to my comment above. The problem is that your Tab class has declared a copy constructor but has nothing in the body of the contructor, so this is why when you place your objects in the vector, they appear to do nothing. If you have access to a c++11 compatible compiler, you can do:

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

In the tabs class, add this constructor:

explicit Tab(Tab &&other) = default;

This is more efficient than copying because it will take the object you currently have and place it into the vector rather than copying it thus creating 2 copies of the same object

Ex: http://rextester.com/MNB8052

smac89
  • 39,374
  • 15
  • 132
  • 179
  • 1
    How will that change anything? I think you are misunderstanding what `std::move` does. It only casts the value into an rvalue, it will not magically move types, especially if they don't have a move constructor. – PeterT Oct 07 '14 at 15:32
  • Thanks. I'll look at it when I get home. I normally don't use the built in class maker, but i did this time and it made the copy constructor. I'm not even sure how to use it. If I were to use the copy constructor, how would I? I understand that std: :move is better. – Andrue Oct 07 '14 at 15:33
  • It should be noted that you don't save anything by moving value-types like `std::array`. If you need them somewhere else and you can't point to or reference them then they will be copied. – PeterT Oct 07 '14 at 15:46
  • Can you explain the double aperstand in this case? It's not an 'And' statement here, is it? – Andrue Oct 07 '14 at 16:30
  • @Crysis The the double ampersand is used to declare an rvalue reference. If you haven't already, [see here](http://stackoverflow.com/questions/5481539/what-does-t-double-ampersand-mean-in-c11) – smac89 Oct 07 '14 at 17:29