Why does the compiler not see the default value?

Observe the following interface / implementation:

properties.h

class Property
{
  public:
    Property(PropertyCollection * propertyCollection, std::string key, 
      std::string value, uint32_t identifier);

properties.cpp

Property::Property(PropertyCollection * propertyCollection, 
  std::string key, std::string value, uint32_t identifier = 0)
  : propertyCollection(propertyCollection), key(key), value(value), 
    identifier(identifier) {}

As you can see, I have a default initializer for the last argument.

However, I still get this Eclipse error:

main.cpp

Properties properties (*file);
Property property (&properties, std::string("Cats"), std::string("Rule"));

there is no corresponding function to call "Property :: Property (Properties *, std :: string, std :: string)

EDIT: Propertiesinherited from PropertyCollection. PropertyCollectionis a pure virtual class.

 +----------------------+
 |    <<pure virtual>>  |
 | PropertyCollection   |
 +----------------------+
 |                      |
 +----------------------+
             ^
             |
             +
+-----------------------+
| Properties            |
|-----------------------|
|                       |
+-----------------------+

In Java, I would think of Properties* how * PropertyCollectionand just pass the as-is link. However, in C ++, should I somehow pointer over the base class?

Edit: not necessary. The only problem was the default initializer location.

+4
4

, properties.h , properties.cpp, .

, , ( main), , , ( , properties.cpp).

, ,

Property::Property (PropertyCollection * propertyCollection, std::string key, 
  std::string value, uint32_t identifier);

properties.h, , , , , .


+4

, , :

class Property
{
  public:
    Property(PropertyCollection * propertyCollection, std::string key, 
      std::string value, uint32_t identifier = 0);

, , main Property::Property(PropertyCollection*, std::string, std::string, uint32_t).

+1

The solution has already been provided in response to juanchopanza. I will tell you what the .h file and .cpp file should look like after the change.

properties.h

class Property
{
  public:
    Property(PropertyCollection * propertyCollection, std::string key, 
      std::string value, uint32_t identifier = 0);

properties.cpp

Property::Property(PropertyCollection * propertyCollection, 
  std::string key, std::string value, uint32_t identifier)
  : propertyCollection(propertyCollection), key(key), value(value), 
    identifier(identifier) {}
+1
source

In the constructor:

Property(PropertyCollection * propertyCollection, std::string key, std::string value);

Your constructor:

Property(Property * propertyCollection, std::string key, std::string value);

Pass the address of the object PropertyCollectionor create a new constructor.

0
source

Source: https://habr.com/ru/post/1532053/


All Articles