-5

我有 2 个编译错误,我一直在尝试修复但没有得到任何结果。这是 2 个错误 'string' does not name a type on line 80 `s' undeclared (first use this function) on line 87

有人可以指出我正确的方向吗

#include <iostream>
#include <string>

using namespace std;


//class type declared
  class Meeting
  {
    private:
     string speaker;
     string topic;
     string venue;
     string date;
   public:
    Meeting();
    void setSpeaker(string s);
    void setTopic(string t);
    void setVenue(string v);
    void setDate(string d);
    string getSpeaker();
    string getTopic();
    string getVenue();
    string getDate();

 };

   //member functions for class Meeting
   Meeting::Meeting()
   {
    string speaker = " ";
    string topic = " ";


string venue = " ";
  string date = " ";
}

//mutators to set member variables to vale given
void Meeting::setSpeaker(string s)
{
     speaker = s;
}
void Meeting::setTopic(string t)
{
     topic = t;
}
void Meeting::setVenue(string v)
{
     venue = v;
}
void Meeting::setDate(string d)
{
     date = d;
}


//Accessors to retrieve data from each of the member variables
string Meeting::getSpeaker()
{
    return speaker;
}
string Meeting::getTopic()
{
    return topic;
}
string Meeting::getVenue()
{
    return venue;
}
string Meeting::getDate()
{
    return date;
}


//main using the class
int main()
{
         Meeting mMeeting; //instance of Meeting Class
   Meeting
         string s;     // error `string' does not name a type 
         string t;
         string v;
         string d;

    // Enter Meeting Information
    cout << "Enter Speaker name and surname: ";
         getline(cin, s);     // error `s' undeclared (first use this function) 
         mMeeting.setSpeaker(s);
    cout << "Enter Topic: ";
         cin >> t;
         mMeeting.setTopic(t);
    cout << "Enter Venue: ";
         cin >> v;
         mMeeting.setVenue(v);
    cout << "Enter Date: ";
         cin >> d;
         mMeeting.setDate(d);


    //Display Meeting details
    cout << "\nSpeaker at meeting: " << mMeeting.getSpeaker() << endl;
    cout << "Topic at meeting: " << mMeeting.getTopic() << endl;
    cout << "Venue of meeting: " << mMeeting.getVenue() << endl;
    cout << "Date of meeting: " << mMeeting.getDate() << endl;

 system ("pause");
 return 0;
}
4

3 回答 3

4

那是因为这条线

    Meeting mMeeting; //instance of Meeting Class
   Meeting //this line is creating error, remove this line
     string s;     // error `string' does not name a type 
于 2013-03-23T19:49:27.763 回答
3

除了其他人提到的错误之外,这是一个语义错误:

Meeting::Meeting()
{
    string speaker = " ";
    string topic = " ";
    string venue = " ";
    string date = " ";
}

在这里,您打算为类成员分配 value " ",但实际上您要做的是定义 4 个本地字符串变量并使用" ". 要获得预期的结果,您应该这样做:

Meeting::Meeting()
{
    speaker = " ";
    topic = " ";
    venue = " ";
    date = " ";
}

上面的函数体按照您的意图为 4 个类成员分配了值。

于 2013-03-23T19:51:05.560 回答
0

删除Meeting第 79 行,它会编译。

于 2013-03-23T19:49:19.723 回答