Reputation: 75
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.
Upvotes: 0
Views: 75
Reputation: 24936
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
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?
Your B.h
does not include but use string
which I suspect to mean std::string
.
Upvotes: 4
Reputation: 5730
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.
Upvotes: 1