0

I've been searching through other forums and questions but I can't seem to find an answer that relates to my issue. I keep getting this error that says "Redefinition of 'Shape'" in the .cpp file and it comes up for both constructors and functions.

Shape.h

#ifndef SHAPE_H
#define SHAPE_H

#include <iostream>
using namespace std;


class Shape {
private:
    string name;
public:
    Shape();
    Shape(string name);
    string getName() const;
    friend ostream& operator << (ostream& output, const Shape & shape);
};

#endif // SHAPE_H

Shape.cpp

#include <iostream>
#include "Shape.h"
using namespace std;

Shape::Shape() {
    this->name = "Shape";
}

Shape::Shape(string name) {
    this->name = name;
}

string Shape::getName() const {
    return name;
}

ostream& operator << (ostream& output, const Shape & shape) {
    output << shape.getName();
    return output;
}
amanhasnoname
  • 236
  • 1
  • 4
  • 16
  • 1
    Unable to to reproduce, so we're going to need some more information. Be really careful when putting `using namespace std;` into a header. It can be a really nasty surprise to someone not expecting it. – user4581301 Apr 16 '18 at 04:49
  • sounds like this code worked correctly. – moinmaroofi Apr 16 '18 at 04:53
  • I was given this code by my professor to begin a project and I haven't touched it so I was very confused as to why it gave me these errors. If I had more to go on I would give it to you. – amanhasnoname Apr 16 '18 at 04:54
  • 1
    do you have a `main()`? are you including `"shape.h"` or `"shape.cpp"`? – kmdreko Apr 16 '18 at 04:56

1 Answers1

0

I keep getting this error that says "Redefinition of 'Shape'" in the .cpp file ? No Shape is defined only once, just make sure Both are different files. And comments below 2 lines from Shape.cpp

//#include <iostream> /* already included in shape.h */
#include "Shape.h"
//using namespace std; /* already there in shape.h */

And you may need to call like below

int main() {
        Shape obj("Nick Morin");
        using std::cout<<obj;
        return 0;
}

Read this post Why is "using namespace std" considered bad practice?

Achal
  • 11,821
  • 2
  • 15
  • 37
  • It was the two comments you mentioned that were causing the issue. I didn't realize something like that could affect it. – amanhasnoname Apr 16 '18 at 05:04
  • 1
    @nick_eagles Neither of those should have had any effect without something else going wrong. `#include ` in both files should't have any effect. The header guard protects from multiple includes. Multiple `using namespace std`s also has no effect. [The damage is already done.](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – user4581301 Apr 16 '18 at 05:28
  • including header twice is not the issue. I agree with you @user4581301 – Achal Apr 16 '18 at 05:31
  • Ya I didn't think that was the problem but it's compiling now so I'm not complaining. Thanks guys for your help and sorry for waisting precious coding time. – amanhasnoname Apr 16 '18 at 06:13
  • Hey @nick_eagles , It's your time, not mine. If I had anything more important to do right now, I'd probably be off doing it. Or sleeping. It's getting late over here. – user4581301 Apr 16 '18 at 06:37