-3

I have 3 files: ***.cpp *2 and ***.h *1

once i put into terminal: g++ mymain.cpp landowner.cpp -o mymain it says

error: ‘landowner’ was not declared in this scope 8 | landowner Alex; | ^~~~~~~~~

//landowner.h

#ifndef LANDOWNER
#define LANDOWNER

using namespace std;

void ShowScore();

#endif

//landowner.cpp
#include "landowner.h"
#include <iostream>
class landowner
{
private:
  string name;
  long score;
  int cards[20];
public:
  landowner(){}
  ~landowner(){}

  void ShowScore(){
    cout<<name<<"current score:"<<score<<endl;
  }
  
protected:
  
  
};
//mymain.cpp
#include <iostream>
#include "landowner.h"

using namespace std;

int main(){

  landowner Alex;
  Alex.name = Alex;
  Alex.score = 100;

  Alex.ShowScore();
  
  return 0;
}

which is very confusing. i must have missed something. Can someone help me? Thanks!

Alex
  • 39
  • 3

1 Answers1

1

The class decparation should be placed inside the header.

//landowner.h

#ifndef LANDOWNER
#define LANDOWNER

#include <iostream>
#include <string>

using namespace std;

void ShowScore();

class landowner
{
private:
  string name;
  long score;
  int cards[20];
public:
  landowner(){}
  ~landowner(){}

  void ShowScore(){
    cout<<name<<"current score:"<<score<<endl;
  }
  
protected:
  
  
};

#endif

//landowner.cpp
#include "landowner.h"

// currently we have nothing to implement here

Using using namespace std; (especially in headers) is bad, so it should be removed and the rest part of code should be changed.

c++ - Why is "using namespace std;" considered bad practice? - Stack Overflow

MikeCAT
  • 73,922
  • 11
  • 45
  • 70