编译错误:`string'没有命名类型

时间:2013-03-23 19:46:32

标签: c++

我有2个编译错误,我一直试图修复,而不是任何地方。 这是2个错误 'string'没有在第80行命名类型 `s'未申报(首先使用此功能)在第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;
}

3 个答案:

答案 0 :(得分: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 

答案 1 :(得分:3)

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

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

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

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

上面的函数体为你想要的4个类成员分配值。

答案 2 :(得分:0)

删除第79行的Meeting并进行编译。