Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8776327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:01:38+00:00 2026-06-13T19:01:38+00:00

Here is some representative code that gets the error I’m experiencing: class Data {

  • 0

Here is some representative code that gets the error I’m experiencing:

class Data
{
};

class Table
{
  virtual std::vector<Data*> getData() = 0;
  virtual void putData(Data* dataItem) = 0;
  virtual Data* getData(int index) = 0;
};

class DerivedData : Data
{
};

class DerivedTable : Table
{
  std::vector<DerivedData*> getData() { return myData; } // invalid covariant return type
  void putData(DerivedData *dataItem) { myData.push_back(dataItem); }
  virtual DerivedData* getData(int index) { return myData[index]; } // invalid covariant return type

  std::vector<DerivedData*> myData;
};

Firstly, I don’t quite understand why it is that the override of putData is happy with the parameters being changed, but I can’t change the return type for getData, although I appreciate this is something I can gain an understanding of from more reading.

Secondly, and my main question, how could this code be changed to make it work. My basic goal is to allow for multiple “table” like objects which will store and control data objects. While each data object will share some things in common, there will be some distinct differences which the table will control and work with. For example, one table might have data objects which have a name parameter, and so the table will provide a function which prints a list of all the names of the data it holds. This way I can have generic code which works with all of these table objects, and specialized code which operates only with one type of table.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:01:39+00:00Added an answer on June 13, 2026 at 7:01 pm

    Many things are wrong:

    1. The two versions of putData are simply different, unrelated overloads. There is no such thing as a “contravariant argument type” for overriding virtual functions in C++ (and even if there were, it would go the other way round!!). Add the keyword override to the derived function to make your compiler produce an error.

    2. Class templates don’t work the way you think. If template <typename T> class Foo is a class template, then Foo<X> and Foo<Y> are totally different, unlrelated classes, no matter whether X and Y are related in any way.


    As @Beta says, you might just have a simple std::vector<std::unique_ptr<Data>> as your main data structure. But anyway, if you really must have some hierarchy, here’s a possible “solution”:

    #include <memory>
    #include <vector>
    
    struct Data { virtual ~Data() { } };
    
    struct Table
    {
        virtual ~Table() { }
    
        typedef std::unique_ptr<Data> data_ptr;
        typedef std::vector<data_ptr> dataset_type;
    
        virtual dataset_type & getData() = 0;
        virtual void putData(data_ptr dp) = 0;
        virtual Data & getData(std::size_t n) = 0;
    };
    
    class DerivedTable : public Table
    {
        dataset_type myData;
    
    public:
    
        virtual void putData(data_ptr p) override
        {
            myData.push_back(std::move(p));
        }
    
        Data & getData(std::size_t n) override
        {
            return *myData[n];
        }
    
        // ...
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is some code: class Person def initialize(age) @age = age end def age
Here's a simplified version of my code that compiles: #include <iostream> class pos {
Why does the titlebar overlap the pixels of JPanel. Here some code: protected void
Here is some sample Python code: import re some_regex = re.compile(r\s+1\s+) result = some_regex.search(
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
Here's some code (full program follows later in the question): template <typename T> T
Here's some code: DirectorySearcher searcher = new DirectorySearcher(); searcher.Filter = (&(objectClass=user)(sAMAccountName= + lstUsers.SelectedItem.Text +
Currently I have the following code for a project that represents some probability trees
I'm looking for some best practice advice here. I have a library that I'd
I know that for the code below, Illegal below is undefined (while some compilers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.