Parse issue expected { or ,

I'm writing a derived class which calls the superclass constructor but im getting this issue and I cant seem to figure out why. It occurs at line 9
1
2
3
4
5
6
7
8
9
10
#include<string>
#include "Computer.hpp"

class AppleMacbook : public Computer
{
private:
    std::string OS = "OS X";
public:
    AppleMacbook():Computer(std::string newName, int newStorage);//: public Computer(newName, newStorage);
};
Last edited on
changed the class a little bit but still getting same error

1
2
3
4
5
6
7
8
9
10
#include<string>
#include "Computer.hpp"

class AppleMacbook : public Computer
{
private:
    std::string OS = "OS X";
public:
    AppleMacbook(std::string newName, int newStorage) : Computer(newName, newStorage);
};
If you supply a member initializer list, you must also supply a constructor body, even if it is empty:
1
2
3
AppleMacbook(std::string newName, int newStorage) 
  : Computer(newName, newStorage) 
  { };

Last edited on
Ohhhh i see, I was attempting to define it in a .cpp file instead of the .h It worked! Thanks :)
Ohhhh i see, I was attempting to define it in a .cpp file instead of the .h It worked! Thanks :)


You can still put it in the .cpp file:
1
2
3
AppleMacbook::AppleMacbook(const std::string& newName, const int newStorage) 
  : Computer(newName, newStorage) 
  { };


In the .hpp file:

1
2
public:
    AppleMacbook(std::string newName, int newStorage);


That way the user doesn't see whatever code that might be in there.
@TheIdeasMan ohhh I see, i was getting the const error so i thought i was doing it wrong but this makes sense.

I also have one more new error. It says I am putting too many arguments into my function but it appears I have defined it to accept two.

Print in my class printer is a pure virtual function.
virtual void print(std::string nJobName, int numPgs) = 0;

Main.cpp where error is occurring
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int	main() {
    Computer*	mac = new	AppleMacbook("MyMac", 1000);
    Computer*	dell = new	DellDesktop("MyDell", 500);
    EpsonPrinter*	epson = new	EpsonPrinter("MyEpson", 2);
    PowerSource*	source = new	PowerSource();
    source->attach(*mac);
    source->attach(*dell);
    source->attach(*epson);
    mac->addPrinter(*epson);
    dell->addPrinter(*epson);
    mac->scan("Passport	application", 10);
    mac->print("Story", 5);
    dell->scan("Taxes", 25);
    dell->print("License agreement", 2);
    source->printInventory();
}


void print(std::string nJobName, int numPgs);

I can supply more code if it helps but theres about 15 files which relate to each other (not very long tho) so its hard to explain.


Last edited on
Did you define the print function in the derived class? Note I mean the class function not the free function.

The function only needs to be pure virtual if it is going to be different for the derived classes. Otherwise just make it ordinary virtual.

Pass std::string by const reference. Use const where ever you can.

Edit:
If you have errors, then post them here verbatim, and with the code it refers to with the correct line numbers.
Last edited on
Lines 12 & 14 in the above code give me the error "Too many arguments to function call, expected 0, have 2"

Its an abstract class so should i keep it pure virtual? I'm not too good with ADT or virtual functions yet.

1
2
3
4
5
6
class Printer : public ElectronicDevice
{
public:
    virtual void print(std::string nJobName, int numPgs) = 0;
    virtual void scan(std::string nJobName, int numPgs) = 0;
};


derived class:
1
2
3
4
5
6
7
8
9
10
class EpsonPrinter : public Computer, public Printer
{
private:
    std::string OS = "Linux";
public:
    EpsonPrinter(std::string newName, int newStorage) : Computer(newName, newStorage){};
    std::string getOperatingSystem();
    void print(std::string nJobName, int numPgs);
    void scan(std::string nJobName, int numPgs);
};


Definition in derived class:
1
2
3
4
5
6
7
void EpsonPrinter::print(std::string nJobName, int numPgs)
{
    for (int i = 0; i < numPgs; i++)
    {
        std::cout << "Printing page " << numPgs << " of " << nJobName << std::endl;
    }
}
Last edited on
But where is the definition of the print function - that is what does the function do? A pure virtual function must be overridden (redefined) somewhere.

When I say show the error verbatim, I mean directly as it come from the compiler, char for char.

A class can be abstract as long as there is at least 1 pure virtual function, one of the other functions might lend itself towards that better.
I figured it out trying to copy the error lol, When i double clicked it, it took me to where it had no arguments. Thanks For your help tho!

Topic archived. No new replies allowed.