mukul ishwar
mukul ishwar

Reputation: 25

What is this c++ program not executing?

I am testing the concept of putting classes in separate files for the first time and while executing getting an error. Please help

main.cpp this is the main file

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

    int main()
    {
       newClass obj1("mayan");
      cout << obj1.doneName() << endl ;


    }

newClass.h this is the separate header file

#ifndef NEWCLASS_H
#define NEWCLASS_H

#include <iostream>
#include <string>
#include <string>
class newClass{

private:
    string name;

public:
    newClass(string z) ;

     string doneName();

};


#endif // NEWCLASS_H

and this is the separate newClass.cpp file

#include "newClass.h"
#include <iostream>
#include <string>

using namespace std;
newClass::newClass(string z)
{
   name = z ;
}

 string newClass :: doneName()
 {
     return name;
 }

Upvotes: 0

Views: 1118

Answers (1)

You need to read more about C++ and its compilation. Read more about the linker.

Notice that a C++ source file is a translation unit and usually includes some header files. Read more about the preprocessor.

You'll better use std::string not just string in a header file (because having using std; is frowned upon in header files).

Don't forget to enable all warnings and debug info when compiling. With GCC, compile with g++ -Wall -Wextra -g.

In practice, you'll better use some build automation tool such as GNU make when building a project with several translation units.

Remember that IDEs are just glorified source code editors able to run external tools like build automation tools, compilers, debuggers, version control systems, etc... You'll better be able to use these tools on the command line.

Upvotes: 2

Related Questions