-2
 #ifndef constructors_h_
    #define constructors_h_
    #include<iostream>


    using namespace std;
    class person{
        private:
        int age();
        string name();
        public:
        person();
        person(string newName){
            name=newName; age=0;};
            string tostring();
    

    };
    #endif constructors_h_


I am completely new to coding a was trying to use use string stream but my compilers keeps showing this problem(vs code)

zeus31121
  • 1
  • 5
  • 2
    Why are `age` and `name` functions? Did you meant to make them data members? – Yksisarvinen May 02 '23 at 08:27
  • Please copy and paste ***the exact error message*** that you receive, and indicate precisely which line of code is the line that the error message points to. Before you do that, format your code so that every statement is on a separate line. (In other words, properly format your code.) – Mike Nakis May 02 '23 at 08:29
  • Not related : in general don't use `using namespace std;` but absolutely don't use it in a header file! Also if you use std::string, include – Pepijn Kramer May 02 '23 at 08:33
  • Learn C++ from : [a recent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or from [learncpp.com](https://www.learncpp.com/) that site is decent and pretty up to date. Then use [cppreference](https://en.cppreference.com/w/) for reference material (and examples). When you have done all that keep using [C++ core guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) to keep up to date with the latest best practices since C++ keeps evolving. – Pepijn Kramer May 02 '23 at 08:33
  • Read about **vexing parse** and see [Why can't member initializers use parentheses?](https://stackoverflow.com/questions/24836526/why-cant-member-initializers-use-parentheses) – Jason May 02 '23 at 08:38
  • Among the *many* things wrong in this code, I suspect you also need to learn what proper preprocessor syntax is, because `#endif constructors_h_` .. isn't. You're also assuming `#include ` pulls in ``, which is not guaranteed. Whilst removing `using namespace std;` add [`#include `](https://en.cppreference.com/w/cpp/header/string) to properly pull in the required header. Wherever/whatever you're learning c++ from, find an alternative. Get a good book, spend time working through it, etc. – WhozCraig May 02 '23 at 08:39
  • so let me get this i need to stop using namespace std; and i need to use a new course of coding #include might not pull in string – zeus31121 May 02 '23 at 12:22

2 Answers2

0

You probably want the age and name to be variables and not functions and to initialize them with (), however that makes them function declarations. Either remvoe the (), or use {} instead, if you want to initialize them.

And you might want to take a const std::string& in the constructor instead of just a std::string to avoid copying of the whole string.

And please never ever use using namespace in a header file. You shouldnt do it in C++ files, but never ever in header files, because headers are just copied into your C++ files including the header by the compiler, and therefore you have the using in each of those C++ files, which may lead to conflicts with other functions. For more information see this post on using namespace std.

Joel
  • 721
  • 1
  • 13
-1

You are assigning string object newName to function string name() and integer zero to function int age(), which is obviously not allowed and probably not what you want to do.

jiandingzhe
  • 1,881
  • 15
  • 35