Hi out there.

I can't get the constructors in my header files to work.

Here's a copy of my header file code:

#ifndef GUARD_PHEADER_H
#define GUARD_PHEADER_H

//The header file for the Person.cpp.
//person_header.h

#include <iostream>
#include <string>

//Person class declaration
class Person {
public:
       std::istream& read_surname(std::istream&);
       std::istream& read_name(std::istream&);
       std::istream& read_phone(std::istream&);
       std::istream& read_age(std::istream&);
       std::istream& read_weight(std::istream&);
       std::string& read_phone(std::string&);
       int& read_age(int&);
       double& read_weight(double&);
       std::string get_name() const { return name; }
       std::string get_surname() const { return surname; }
       std::string get_phone() const { return phone; }
       int get_age() const { return age; }
       double get_weight() const { return weight; }
       person(std::string&, std::string&, std::string&, int&, double&);
       Person(std::ifstream&);
private:
        std::string surname;
        std::string name;
        std::string phone;
        int age;
        double weight;
    };    
#endif

And a copy of the function code:

//Constructor for Person

Person::person(std::ifstream& in) : surname(0), name(0), phone(0), age(0), weight(0.0) { }

//Constructor for Person
Person::person(): surname(0), name(0), phone(0), age(0), weight(0.0) { }

I keep getting all the weird errors that you can find in my compile log. I'm running Dev C++ on Windows XP.

Thanx:cheesy:
Steven

Dani AI

Generated

Quick diagnosis: the compiler errors come from a name/signature mismatch and a bad initializer. As noted, C++ is case-sensitive. In ’s header a constructor was declared with the wrong identifier (lowercase) so the compiler treats it as a malformed member declaration. That explains the "ISO C++ forbids declaration ... with no type" and the "only constructors take base initializers" messages: the compiler thinks those are ordinary functions, not constructors, so the initializer lists are illegal.

Concrete fixes and common pitfalls:

  • Make the constructor name exactly the same as the class: Person, no return type. Declarations and definitions must match exactly (name, parameter types, const/ref qualifiers).
  • Don’t initialize std::string members with 0 (null). Use "", std::string(), or let the member default-construct.
  • If a constructor parameter uses std::ifstream in the header, include <fstream> (or accept a std::istream& instead) so the type is known.
  • Prefer const std::string& for string parameters; pass int and double by value rather than by non-const reference unless there’s a reason.

Minimal corrected example (header + definitions):

// in header: include <fstream> if using std::ifstream
class Person {
public:
    Person(const std::string& surname, const std::string& name,
           const std::string& phone, int age, double weight);
    Person(std::istream& in);
private:
    std::string surname, name, phone;
    int age;
    double weight;
};
Person::Person(const std::string& s, const std::string& n,
               const std::string& p, int a, double w)
  : surname(s), name(n), phone(p), age(a), weight(w) { }

Person::Person(std::istream& in)
  : surname(), name(), phone(), age(0), weight(0.0) { /* read from in */ }

Quick troubleshooting checklist: fix the constructor name case first, then make signatures identical between header and implementation, add the proper include for stream types, and rebuild with warnings enabled (e.g., -Wall) to catch remaining problems.

Recommended Answers

All 3 Replies

Oh Yeah, and here's my compiler log...

Compiler: Default compiler
Building Makefile: "C:\Documents and Settings\Steven\Desktop\CTI\C++ Unit 3\Finished Project\Makefile.win"
Executing make...
make.exe -f "C:\Documents and Settings\Steven\Desktop\CTI\C++ Unit 3\Finished Project\Makefile.win" all
g++.exe -c person.cpp -o person.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

In file included from person.cpp:8:
person_header.h:26: error: ISO C++ forbids declaration of `person' with no type
person.cpp:68: error: ISO C++ forbids declaration of `person' with no type
person.cpp:68: error: prototype for `int Person::person(std::ifstream&)' does not match any in class `Person'
person_header.h:26: error: candidate is: int Person::person(std::string&, std::string&, std::string&, int&, double&)
person.cpp: In member function `int Person::person(std::ifstream&)':
person.cpp:68: error: only constructors take base initializers
person.cpp: At global scope:
person.cpp:71: error: ISO C++ forbids declaration of `person' with no type
person.cpp:71: error: prototype for `int Person::person()' does not match any in class `Person'
person.cpp:68: error: candidates are: int Person::person(std::ifstream&)
person_header.h:26: error: int Person::person(std::string&, std::string&, std::string&, int&, double&)
person.cpp: In member function `int Person::person()':
person.cpp:71: error: only constructors take base initializers

make.exe: *** [person.o] Error 1

Execution terminated

Oh yeah, the error on line 26 occurs in the header and 68 and 71 occur in the definition of the functions

Remeber that C++ is case-sensitive (uppercase and lowercase matter).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.