头文件中的c++编译错误

C++ compilation errors in header

本文关键字:编译 错误 c++ 文件      更新时间:2023-10-16

提醒一下,这是学校的作业,但不计分。

我提供了这4个源代码文件,但头似乎阻碍了我与字符串/函数错误等。

代码块

super.h

    class super
{
public:
    ///default constructor
    super(){}
    ///alternate constructor
    super(string i, string a, string m, string l)
        {Ident = i; Alias = a; MState=m; Location=l;}
    ///destructor
    ~super(){cout<<"BOING!! Hero Disappears"<<endl;}
    ///mutators  -- Sets
    void setIdent(string i){Ident = i;}
    void setAlias(string a){Alias = a;}
    void setMState(string m){MState = m;}
    void setLocation(string l){Location = l;}

    ///accessors  -- Gets
    string getIdent(){return Ident;}
    string getAlias(){return Alias;}
    string getMState(){return MState;}
    string getLocation(){return Location;}
    ///other functionality  -- declared here, defined in impl file
    void display();
    void changeLoc();
private:
    string Ident;
    string Alias;
    string MState;
    string Location;
};

super_impl.cpp

///first.....go find the definition of a super.
#include "super.h"
///this file contains the "guts" of
///functions too large to be in the header.
///this is apart of the definition of a SUPER
///a void function display that follows an instance of super
void super::display()
{
    cout<<"The Amazing "<<Alias<<endl;
    cout<<"Mild Mannered "<<Ident<<" seeks ";
    cout<<MState<<endl;
    cout<<"Currently found at "<<Location<<endl<<endl;
}
void super::changeLoc()
{   string stemp;
    cout<<"Where is "<<Alias<<" going?"<<endl;
    getline(cin,stemp);
    Location = stemp;
    cout<<endl;
}

super_test_1.cpp

#include <string>
#include <iostream>
using namespace std;
#include "super_impl.cpp"
int main()
{
    super S1;
    S1.display();
    S1.setIdent("Robby Williams");
    S1.setAlias("Snail");
    S1.setMState("To Protect Invertebrates Everywhere");
    S1.setLocation("His secret lair under the rock");
    S1.display();
    S1.changeLoc();
    S1.display();
cout<<"we are getting ready to call S1's destructor as we leave scope"<<endl;
   return 0;
}

super_test_2.cpp

#include <string>
#include <iostream>
using namespace std;
#include "super_impl.cpp"
int main()
{
    ///sptr is a location that can contain
    /// ONE thing...the address of a super
    super * sptr;
    ///we have an address..but no super..sooooo...
    ///using the alternate constructor
    sptr = new super;
    sptr ->setAlias("temp");
    sptr ->display();
    cout<<"before delete: "<<&sptr<<endl;
    ///the next line calls the destructor of the thing that
    ///sptr is pointing at...sptr is NOT deleted..how weird.
    delete sptr;
    cout<<"After delete: "<<&sptr<<endl;
    sptr = new super("Brian Ye","Proxy", "Save People", "no one knows");
    ///NOW we have a NEW super because sptr STILL EXISTS
    sptr->display();
    sptr->setIdent("Robby Williams");
    sptr->setAlias("Snail");
    sptr->setMState("To Protect Invertebrates Everywhere");
    sptr->setLocation("His secret lair under the rock");
    sptr->display();

   return 0;
}

编译错误
||=== Build: Debug in Simple Super Person Object (compiler: GNU GCC Compiler) ===|
Z:CS250super.h|7|error: expected ')' before 'i'|
Z:CS250super.h|13|error: 'string' has not been declared|
Z:CS250super.h|14|error: 'string' has not been declared|
Z:CS250super.h|15|error: 'string' has not been declared|
Z:CS250super.h|16|error: 'string' has not been declared|
Z:CS250super.h|20|error: 'string' does not name a type|
Z:CS250super.h|21|error: 'string' does not name a type|
Z:CS250super.h|22|error: 'string' does not name a type|
Z:CS250super.h|23|error: 'string' does not name a type|
Z:CS250super.h|30|error: 'string' does not name a type|
Z:CS250super.h|31|error: 'string' does not name a type|
Z:CS250super.h|32|error: 'string' does not name a type|
Z:CS250super.h|33|error: 'string' does not name a type|
Z:CS250super.h||In destructor 'super::~super()':|
Z:CS250super.h|10|error: 'cout' was not declared in this scope|
Z:CS250super.h|10|error: 'endl' was not declared in this scope|
Z:CS250super.h||In member function 'void super::setIdent(int)':|
Z:CS250super.h|13|error: 'Ident' was not declared in this scope|
Z:CS250super.h||In member function 'void super::setAlias(int)':|
Z:CS250super.h|14|error: 'Alias' was not declared in this scope|
Z:CS250super.h||In member function 'void super::setMState(int)':|
Z:CS250super.h|15|error: 'MState' was not declared in this scope|
Z:CS250super.h||In member function 'void super::setLocation(int)':|
Z:CS250super.h|16|error: 'Location' was not declared in this scope|
Z:CS250super_impl.cpp||In member function 'void super::display()':|
Z:CS250super_impl.cpp|11|error: 'cout' was not declared in this scope|
Z:CS250super_impl.cpp|11|error: 'Alias' was not declared in this scope|
Z:CS250super_impl.cpp|11|error: 'endl' was not declared in this scope|
Z:CS250super_impl.cpp|12|error: 'Ident' was not declared in this scope|
Z:CS250super_impl.cpp|13|error: 'MState' was not declared in this scope|
Z:CS250super_impl.cpp|14|error: 'Location' was not declared in this scope|
Z:CS250super_impl.cpp||In member function 'void super::changeLoc()':|
Z:CS250super_impl.cpp|18|error: 'string' was not declared in this scope|
Z:CS250super_impl.cpp|18|error: expected ';' before 'stemp'|
Z:CS250super_impl.cpp|19|error: 'cout' was not declared in this scope|
Z:CS250super_impl.cpp|19|error: 'Alias' was not declared in this scope|
Z:CS250super_impl.cpp|19|error: 'endl' was not declared in this scope|
Z:CS250super_impl.cpp|20|error: 'cin' was not declared in this scope|
Z:CS250super_impl.cpp|20|error: 'stemp' was not declared in this scope|
Z:CS250super_impl.cpp|20|error: 'getline' was not declared in this scope|
Z:CS250super_impl.cpp|21|error: 'Location' was not declared in this scope|
||=== Build failed: 34 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

In super.h:

你应该用std::作为string出现的前缀。coutendl也是一样,它们也来自标准库。您应该在super.h中包含相关的标题。

您还可以添加标题保护,以防止多个包含。

In super_test_1.cpp:

您希望包含super.h而不是super_impl.cpp。为什么?因为如果多次包含super_impl.cpp,同一函数的多个定义可能会导致问题。