0

I'm having a problem when using the class files and the compiler error says "error: 'x' was not declared on this code" while it points out the cout, string, and endl. I have already wrote "#include " and "#include " in both header, class, and main file.

(Sorry for my English) I'm just a beginner and I wanted to know the basics

Added #include and #include in both files

//Main File (main.cpp)
#include <iostream>
#include "test.h"
#include <string>
using namespace std;

int main()
{
    test *person = new person("Phroton",14)
    person.Display();
    return 0;
}

//test.h
#ifndef TEST_H
#define TEST_H
#include <iostream>
#include <string>
class test
{
    private:
        string name;
        int age;
    public:
        void Display(){
            cout << "I'm " << name << " and I'm " << age << "years old" << endl;
        }
};

#endif // TEST_H
//test.cpp (There is no problem with this file at all)
#include "test.h"
#include <iostream>
#include <string>
test::test(string iname, int iage)
{
    name = new string;
    age = new int;
    *name = iname;
    *age = iage;
}

test::~test()
{
    delete name;
    delete age;
    cout << "Info Deleted" << endl;
}
Phroton
  • 1
  • 1
  • 3
    You appear to be misusing `new` 100% of the time in this code. I'd recommend unlearning `new` until you get familiar enough with C++ that it's time to move from self-contained learning code to real-world legacy code. – chris May 08 '19 at 04:55
  • Constructor is also not define in header file. – jiten May 08 '19 at 05:12
  • Hi and welcome to SO. Do you want to stick to the pointers?? – skratchi.at May 08 '19 at 05:13
  • To learn the basics, get [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start with chapter one. Read the text (don't just look at the code), do the exercises, don't skip ahead. Stay away from online tutorials. – molbdnilo May 08 '19 at 06:34

1 Answers1

0

Answering the specific problem you have asked:

This is because you have not specified the namespace cout and endl belong to in the file test.h.

The statement in Display should be:

std::cout << "I'm " << name << " and I'm " << age << "years old" << std::endl;

The alternative to this is the using namespace std declaration but this is considered a bad practice (especially in a header file).

Note:

  • You do not need using namespace std in main.cpp as you are not using any functions from the std namespace there. Even if you do, use the std::name instead of the using declaration.

  • Member function definitions are usually present in .cpp files. So you can define the function Display to test.cpp.

  • Also consider moving away from raw pointers to smart pointers.

P.W
  • 26,289
  • 6
  • 39
  • 76
  • there are too many errors in the code you do not mention... deference `name` and `age` in the `std::cout`, so you see the `value` and not the `pointer address`. For every `new` there has to be a `delete`, concerning the memory leak in main. Declare the member variables as a `pointer` to be able to work. Use `->` in main to access the function. And probably much more... – skratchi.at May 08 '19 at 05:16
  • I was not trying to do a code review but only answer the specific question of the OP. Anyway, I have added a note to move to smart pointers instead of raw pointers. – P.W May 08 '19 at 05:17
  • Solving only one specific problem won't help the OP any further... He will stay with a non functional code and asking why... – skratchi.at May 08 '19 at 05:19
  • 1
    I answered what the OP had asked. OP can post another question for other issues. You are free to add your own answer. Let us see if it will measure up to your own standards of complete resolution of all the problems in the posted code. – P.W May 08 '19 at 05:23