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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:17:12+00:00 2026-05-30T00:17:12+00:00

The template i have is: template <class T> class Shape { T val,val_new; public:

  • 0

The template i have is:

template <class T>
class Shape {
T val,val_new;
public:
Shape(T initval)
{
   val=initval;
}
T get()
{
   return val;
}
void set (T newval)
{
   val_new = newval;
}
void copy()
{
   val= val_new;
}
};

The class to use this template is :

#include <iostream>
#include<math.h>
using namespace std;
class Rectangle
{
 private:
 Shape<TwoPoint> val;
 bool incr_called, decr_called, load_called;
 TwoPoint newval;
 public:
 Rectangle(TwoPoint i)
      : val (Shape<TwoPoint> (i)) {}     
 Shape<TwoPoint> read()
 {
   return val;
 }
 void load(TwoPoint n)
 {
   load_called=1;
   newval=n;
 }
 void increment()
 {
   incr_called=1;
 }
 void decrement()
 {
   decr_called=1;
 }
 void init()
 {
   incr_called=0;
   decr_called=0;
   load_called=0;
 }
 void actions()
 { 
   if (load_called)
       val.set(new TwoPoint(newval));
   if(incr_called && !decr_called)
       val.set((new TwoPoint(val.get())).plus(1));
   if(!incr_called && decr_called)
       val.set((new TwoPoint(val.get())).plus(-1));
 }     
 };

TwoPoint class is defined as:

 class TwoPoint
 {
   int width;
   int value;
   public:
    TwoPoint()
    {
      value=0;
      width=0;
     }
     TwoPoint(int v, int w)
     {
       value=v;
       width=w;
     }
     TwoPoint(const TwoPoint& t)
     {
       value= t.value;
       width= t.width;
     }
     int getWidth()
     {
       return width;
     }
     int getValue()
     {
       return value;
     }
     TwoPoint & plus(int newval)
     {
      value+=newval;
      return *this;
     }
    };

But there are errors:

In member function 'void Rectangle::actions()':
error: request for member 'plus' in '(((TwoPoint*)operator new(8u)), (((*<anonymous>)
<unknown operator> TwoPoint<T>::get() [with T=TwoPoint]()), <anonymous>))', which is of non-class type 'TwoPoint*'

There is another error:

In member function 'void Rectangle::actions()':
error: no pattern matching function for call to 'Shape<TwoPoint>::set(TwoPoint*)'
note: candidates are: void Shape<T>:: set<T> [with T=TwoPoint]

There are two more errors when i am doing similar operations in actions() due to same reason. Can somebody please explain these errors and how to improve them?
Is there any way in which i can make code more efficient?

  • 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-05-30T00:17:14+00:00Added an answer on May 30, 2026 at 12:17 am

    Shape::set takes its argument by value, but you’re creating a value using new and passing a pointer. You should avoid using new unless you actually need a dynamic object; in which case, you need to make sure it’s deleted when you’ve finished with it.

    In this case, you just want to pass an object by value:

     void actions()
     { 
       if (load_called)
           val.set(newval);
       if(incr_called && !decr_called)
           val.set(val.get().plus(1));
       if(!incr_called && decr_called)
           val.set(val.get().plus(-1));
     }     
    

    Is there any way in which i can make code more efficient?

    Dynamic allocation is usually less efficient than using automatic objects – fixing the error has also removed a source of inefficiency.

    Shape::set, and the constructor, could take their arguments by reference, and Shape::get could return a reference, to avoid unnecessary copying; although in practice the compiler will probably avoid those copies anyway. Also, the constructor could use an initialiser list, to initialise the member(s) directly, rather than default-initialising them and the reassigning them. Code like this might be marginally more efficient in some cases:

    Shape(T const & initval)      // pass by reference
      : val(initval)              // use initialiser list
    {}
    T const & get()               // return by reference
    {
       return val;
    }
    void set (T const & newval)   // pass by reference
    {
       val_new = newval;
    }
    

    But in general, focus on making the code correct and readable, and on choosing efficient algorithms; only worry about small inefficiencies if they prove to be a bottleneck.

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

Sidebar

Related Questions

Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}
I have a class template <typename Iterator, typename Value> class Foo { public: Foo(const
I have a template class where I want to use objects of that class
I have: class A { public: B toCPD() const; And: template<typename T> class Ev
I have a class template that intends to use its parameter K as the
I have a template class that I serialize (call it C), for which I
I have a template class like below. template<int S> class A { private: char
I have a template class that is only valid for couple of template parameters:
I have a template class for thread-safe vector: template <class T> class SharedVector {
I have a template class defined like so: template <class T> class Command {

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.