0

Below are my 3 cpp files and 2 header files. I received an astronomical amount of errors and most are very unclear. I am very new to c++ and have a C#/Java background.

Its clear to me that the below are likely syntax errors. Thanks for the help in advance.

Main.cpp:

#include <iostream>
#include "B.h"
#include "S.h"

using namespace std;

int main() {
    B b;
    S s("Jon");
    return 0;
};

B.h:

#ifndef B_H
#define B_H

class B {
    public:
        B();
};

#endif

B.cpp:

#include <iostream>
#include <string>
#include "B.h"
#include "S.h"

using namespace std;

class B {
    public:
        B() {}
};

S.h:

#ifndef S_H
#define S_H

class S: public B {
    public:
        S(string name);
}
#endif

S.cpp:

#include <iostream>
#include <string>
#include "B.h"
#include "S.h"

using namespace std;

class S: public B {

    private:
        string s;

    public:
        S(string name) {
            s = name;
        }
};

Here is my huge list of errors. It's a little overwhelming.

enter image description here

Jon Perron
  • 83
  • 9

2 Answers2

4

1) You cannot define a class in a header and redefine it different in some source file.

A (forward) declaration of a class is a statement like:

class X;

A definition of a class is something like:

class X
{
  // stuff
};

Such definition may only appear once for each class.

If you do not want to have the data members as part of you public interface you can either

  1. use an opague pointer to completely hide them from the header or
  2. make those members private.

B.h

#indef B_H
#define B_H
#include <string> // to be used here, so we need to include it
// not "using namespace std;" here!! *
class B 
{
public:
    B();
    void setValues();
    std::string printValues() const; // don't miss that std::
private:
    std::string s, result;
    float f;
    int i;
    bool b;
};
#endif

B.cc

#include "B.h"

B::B() 
    : f(), i(), b() // **
 { }

void B::setValues() { }

std::string printValues() const
{
    result = s + " " + std::to_string(i) + " " + 
        std::to_string(f) + " " + std::to_string(b);
    return result;
}

S.h

#ifndef S_H
#define S_H
#include "B.h" // required to make B known here
#include <string> // known through B, but better safe than sorry
class S : public B 
{
public:
    S(std::string name);
    std::string subPrint() const; // ***
};
#endif

S.cc

#include "S.h"

S::S(std::string name) 
    : s{name} // **
{ }

std::string subPrint () const // ***
{
    return printValues() + s;
}

*: Why is “using namespace std” in C++ considered bad practice?

**: C++, What does the colon after a constructor mean?

***: Meaning of “const” last in a C++ method declaration?

2) You have to include required headers everywhere you use the types.

Your B.h does not include but use string which I suspect to mean std::string.

Community
  • 1
  • 1
Pixelchemist
  • 24,090
  • 7
  • 47
  • 71
  • Thanks! Believe it or not you're a better teacher than my school has hired. I'm gonna fool around with this for a bit and better understand it. Thanks for the extra links, I'm gonna take a look at them. If I dont have anymore questions I'll confirm the question completed. Thanks again! – Jon Perron Apr 17 '16 at 23:17
  • I've modified the variables in the base class and made them protected, added a 5th variable only in the subclass 'S'. The current error im having is that all variable in printValues() and subPrint() are coming up with an "undeclared identifer" error. This includes s, i, f, b, s2 and the function printValues() – Jon Perron Apr 17 '16 at 23:51
1

Well, you have a lot of errors in your code. My suggestion is to go one-by-one down those errors and look at the lines identified as the culprit.

I also suggest you review how to declare a class and how to define its members. For example, B.h and B.cpp both define a class B, but do so in different ways. Then S.h redefines class B.

Your code is too broken for us to fix it piece-by-piece. You need to restart after reviewing areas of C++ that are confusing to you, such as declaring and defining classes and their members. Wikipedia has a good introduction. Remember, when a definition is separate from the declaration, you don't use class S { ... } again, you use S::member format to introduce the definition.

rsjaffe
  • 5,600
  • 7
  • 27
  • 39
  • I've updated it to remove a lot of the bloat and fix some things. I have less errors, but I'm still confused. – Jon Perron Apr 17 '16 at 22:53