0

I am working on some code to make a wallet to hold different currencies and this is my first time programming in c++ as a c programmer. Every time I make a new currency I want to add it to the list of valid Currencies that my wallet will be able to hold. To do this I make a currency class with a list that I want to add to every time a new currency is spawned. The error I get is error: no matching function for call to ‘std::__cxx11::list<Currency>::push_back(Currency*) CurrencyList.push_back(this);"\
Currency.h looks like:

#ifndef CURRENCY_H
#define CURRENCY_H

#include <string>
#include <list>

class Currency {
    public:
        //Instances of class
        int id;
        float max;
        float left_over;
        std::string coinName;
  
        //Methods
        float buyFrom(float amount);
        float sellBack(float amount);

        //constructor
        Currency();
};

extern std::list<Currency> CurrencyList; //global list

#endif 

Currency.c looks like

#include "currency.h"
#include <iostream>

Currency::Currency() {
    Currency::id = 0;
    std::cout << "Input name :" << std::endl;
    std::cin >> Currency::coinName;
    std::cout << "Input max :" << std::endl;
    std::cin >> Currency::max;
    Currency::left_over = Currency::max - 0;
    CurrencyList.push_back(this);
}

float Currency::buyFrom(float amount) {
   Currency::left_over-=amount;
   std::cout << "Currency just lost :" << amount << "remaining is : " << Currency::left_over << std::endl;
}

float Currency::sellBack(float amount) {
    Currency::left_over -= amount;
    std::cout << "Currency just gained : " << amount << " remaining is : " << Currency::left_over << std::endl;;
}

The main is quiet simple it is only meant to spawn an object to test, that looks something like this.
Main.cpp

#include <iostream>
#include "wallet.h"
#include "currency.h"

int main(){
    std::cout << "Hello World" << std::endl;
    Currency currencyTest;
    currencyTest.buyFrom(200.3);
}
Chris
  • 26,361
  • 5
  • 21
  • 42
  • 1
    Immediate (but fundamentally wrong) solution is to make `CurrencyList` a list of pointers to `Currency` objects. Better would be to make `std::list> CurrencyList;` – JohnFilleau Nov 12 '21 at 15:55
  • 1
    Is this for a school project? There are several issues in your design and implementation, and I feel like your teaching staff could help with both of those if you asked. – JohnFilleau Nov 12 '21 at 16:01
  • I learned C++ coming from a C background. I did not have a [good C++ book](https://stackoverflow.com/a/388282/4641116), because back then C++ was too new. But these days, there are plenty of good ones, and alas many that are subpar. – Eljay Nov 12 '21 at 16:09
  • This looks like ir might be a job for a factory function. The factory could construct the `Currency` inside `CurrencyList` with `emplace_back` and return a reference. Maybe the constructor is `private` and the factory is a `static` `Currency` member function. – user4581301 Nov 12 '21 at 16:14

1 Answers1

0

Note that this is a pointer, but your list holds actual objects, not pointers.

So just dereference the pointer and you should be fine:

CurrencyList.push_back(*this);
SebDieBln
  • 3,303
  • 1
  • 7
  • 21
  • When Running the code I still get the error "In function `Currency::Currency()': undefined reference to `CurrencyList[abi:cxx11]'" – 001brandon Nov 12 '21 at 15:56
  • 1
    @001brandon you've declared your `CurrencyList` object, and told anything that includes the `.h` file that "`CurrencyList` will be defined somewhere. It's not defined here, but when the program is linked together, look for something called this." You need to define it. Usually in the `.cpp` file. – JohnFilleau Nov 12 '21 at 15:59
  • 1
    @JohnFilleau `push_back(*this)` will call the copy constructor, not the default constructor. It is unclear from the post if the intent is to be able to access these constructed objects, of the the list should hold a copy of them. – 1201ProgramAlarm Nov 12 '21 at 16:12
  • @1201ProgramAlarm roger. Deleted the incorrect comment. – JohnFilleau Nov 12 '21 at 16:16
  • 1
    Still worth noting that the asker will have two copies of the instance, one in the list and one in the grubby little hands of the function that created it. Changes made to one will NOT be reflected in the other. – user4581301 Nov 12 '21 at 16:40