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 7781841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:21:17+00:00 2026-06-01T19:21:17+00:00

The example below defines a basic podtype container class. Using this class a series

  • 0

The example below defines a basic podtype container class. Using this class a series of typedefs are then created which represent an OOP version of the basic podtype. The problem originates when we start assigning those types to one another.

I tried to define the operator as friend method with lhs and rhs arguments using plain PodObjects as type but without any succes. Is there anyone who might have experienced something simular or knows an other solution for this problem.

Thanks in advance.

#include <stdint.h>

template <typename T>
class PodObject {
protected:
    T _value;

public:
    PodObject<T>(int rhs) {
        this->_value = static_cast<T>(rhs);
    }   

    PodObject<T> operator+= (PodObject<T> const &rhs){
        this->_value = rhs._value;
        return *this;
    }   
};  

typedef PodObject<int8_t> Int8;
typedef PodObject<int16_t> Int16;

int main() {
    Int16 a = 10; 
    Int8 b = 15; 

    a += b; // Source of problem
    return 0;
}

Results in a compiler output:

example.cpp:26:11: error: no viable overloaded '+='
        a += b;
        ~ ^  ~
example.cpp:13:22: note: candidate function not viable: no known conversion from 'Int8' (aka 'PodObject<int8_t>') to 'const PodObject<short>'
      for 1st argument
        PodObject<T> operator+= (PodObject<T> const &rhs){

EDIT:

The friend method below does the job for me:

template<typename U, typename W>
friend PodObject<U> operator+= (PodObject<U> &lhs, PodObject<W> const &rhs) {
    lhs._value += rhs._value;
    return lhs;
} 
  • 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-01T19:21:18+00:00Added an answer on June 1, 2026 at 7:21 pm

    You need a templated operator + because you are trying to add different types:

    template <typename U>
    PodObject<T> operator+= (PodObject<U> const &rhs){
        this->_value = rhs._value;
        return *this;
    }
    

    That said, the whole code looks like an anti-pattern. Your “OOP version of the basic podtype” is not a meaningful, nor generally useful, concept.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model defined as below: class Example(models.Model): user = models.ForeignKey(User, null=True) other
this example below works when hover event is trigered and when its not, its
The example below shows the code I am using to test whether a user
In my example below I'm using a dijit.form.DateTextBox : <input type=text name=startDate dojoType=dijit.form.DateTextBox constraints={datePattern:'MM/dd/yyyy'}
If I create a JQuery widget (code example below), and then define a public
Consider the following class DialgBean.java, which defines the properties of a dialog box on
I have a few tables that I've defined like the below examples: class TableA
The example below throws an InvalidOperationException, Collection was modified; enumeration operation may not execute.
The example below, is just an example, I know that I don't need an
JSFIDDLE EXAMPLE BELOW I want to make the following CSS layout and currently im

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.