Making an overloaded operator recognize a base class pointer's pointed object type

I'm having lots of trouble getting

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15


int main()
{
  attribute *aPtr, *bPtr;
  aPtr = new size;
  aPtr->is = 1;
  bPtr = new shape;
  bPtr->is = 3;
  hypothesis kPoth(aPtr, bPtr);
  block aBlock(true, 1, 2, 1);
  
  if(aBlock.szAttOne == *(kPoth.first)) std::cout << "Works" << std::endl;
  return 0;
}

to work properly. I keep getting an error where gcc won't recognize my type as anthing but the attribute base class, but I would love for the type of the pointed object to be used instead.

HALP11!1!

heres the error messages


attblockimp.cpp: In function ‘int main()’:
attblockimp.cpp:231:38: error: no match for ‘operator==’ in ‘aBlock.block::szAttOne == * kPoth.hypothesis::first’
attblockimp.cpp:231:38: note: candidates are:
attblockimp.cpp:14:6: note: virtual bool size::operator==(size)
attblockimp.cpp:14:6: note: no known conversion for argument 1 from ‘attribute’ to ‘size’
attblockimp.cpp:24:6: note: virtual bool size::operator==(color)
attblockimp.cpp:24:6: note: no known conversion for argument 1 from ‘attribute’ to ‘color’
attblockimp.cpp:19:6: note: virtual bool size::operator==(shape)
attblockimp.cpp:19:6: note: no known conversion for argument 1 from ‘attribute’ to ‘shape’

The classes are written such that aBlock's szAttOne member is a size type while kPoth's first member is an attribute pointer. There are overloaded operators for each class size, shape, and color are derived from attribute. Attribute is an abstract base class. The second error message shows the most relevant information for my question because it is trying to resolve this as (size == attribute) which isn't an overloaded operator functionm while I want it to recognize the object pointed to by the attribute pointer, which is a size type variable.
Last edited on
Will you please provide more information on the classes attribute, size and shape.
So I just made a workaround where i just included a member to show type in each of the derived classes, but I am definitely still in the market for a clean method by which a pointer returns the object type for overloaded operators and whatnot.
I highly doubt this resembles your custom class code, but you gave so little of your code... This code printed "Works". Perhaps you could provide more of your code that is relevant to your question and I could be of further help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>

class attribute
{
   public:
      int is;
};

class size : public attribute
{

};

class shape : public attribute
{

};

class color : public attribute
{

};

class hypothesis
{
   public:
      attribute* first;
      attribute* second;
      
      hypothesis(attribute* first, attribute* second)
      {
         this->first = first;
         this->second = second;
      }
};

class block
{
   public:
      size szAttOne;
      
      block(bool flag, int att1, int att2, int att3)
      {
      }
};

bool operator== (attribute left, attribute right)
{
   return true;
}

int main(int argc, char** argv)
{
   attribute *aPtr, *bPtr;
   aPtr = new size;
   aPtr->is = 1;
   bPtr = new shape;
   bPtr->is = 3;
   hypothesis kPoth(aPtr, bPtr);
   block aBlock(true, 1, 2, 1);
   
   if(aBlock.szAttOne == *(kPoth.first))
   {
      std::cout << "Works" << std::endl;
   }
   return 0;
}
Topic archived. No new replies allowed.