【问题标题】:Unable to remove missing ; before variable name error无法删除缺失的;变量名错误之前
【发布时间】:2011-06-30 03:22:03
【问题描述】:
#ifndef PRODUCTS_H
#define PRODUCTS_H
#include "Products.h"
class Products
{
protected:
    static int count;
    string name_;
    float cost_;
public:
    Products()  // default ctor
    {
        name_ = "";
        cost_ = 0.0f;
        count++;
    }
    Products(string name , float cost)  //parametorized ctor
    {
        name_ = name;
        cost_ = cost;
        count++;
    }
    Products(Products &p )
    {
        name_ = p -> name_;
        cost_ = p -> cost_;
    }

    ~Products()
    {}

    string getName()
    {
        return name_;
    }
     void setName(string name)
    {
        name_=name;
    }
    float getCost()
    {
    return cost_;
    }
    void setCost(float cost)
    {
        cost_=cost
    }
    float CalcTotal(Products *p_products)   //Not made yet!
    {
        float total=0.0f;
        for(int i = 0 ; i < count; i++)
        {
            total += p_products->cost_;
            p_products ++;
        }
        return total;

    }
    Products read()
    {
        Products size,*p_products;
        cout << "Enter Number of Items To Enter:";
        cin >> size;
        cout << endl;
        p_products = new Products[size];
        for(int i = 0 ; i < size; i++)
        {
            cout << "Enter Name:";
            cin >> p_products -> name_;
            cout << endl << "Enter Cost";
            cin >> p_products -> cost_;
            cout << endl;
            p_products ++;
        }
        return p_products;
    }
    void write(Products *p_products)
    {
        for(int i = 0 ; i < count ; i++,p_products++)
        {
            cout<<"Products Name:\t\t"<<p_products->name_<<endl;
            cout<<"Products Price:\t\t"<<p_products->cost_<<endl;
        }
    }
};
#endif

我的源代码是:

#include <iostream>
#include <string>
#include "Products.h"
using namespace std;
static int Products::count;//declaring static variable
int main()
{
    Products *p_products,temp;
    *p_products=temp.read();
    //temp.write();
    system("pause");
    delete[] Product;
    return 0;
}

但是我收到了这个我无法删除的错误:

错误 C2146:语法错误:缺失 ';'在标识符“name_”之前

请帮帮我!谢谢

【问题讨论】:

  • 以上源文件中的哪一行出现此错误?
  • 指的是我的类中的字符串变量声明
  • 这是您得到的唯一错误吗?这段代码看起来有很多错误。我建议你继续编写一小部分代码并不断重新编译它以保持它没有错误......
  • @peoro:我收到大量错误。这只是其中一个难以调试的错误。

标签: c++ compiler-errors compilation


【解决方案1】:

您应该在第一个文件中包含字符串头文件。看起来它在抱怨它不知道字符串是什么。

你需要添加

#include <string>

并将 name_ 的类型更改为

std::string name_;

【讨论】:

  • @fahad:正是我在chat告诉你的内容
【解决方案2】:

Products &amp;p 中,p 是对Products 类型对象的引用,它不是指针。

您必须使用operator . 而不是operator -&gt; 才能访问参考字段:

Products(Products &p )
{
    name_ = p -> name_;
    cost_ = p -> cost_;
}

【讨论】:

    【解决方案3】:

    尝试移动这条线:

    using namespace std;
    

    在您#include "Products.h" 的行上方。但是,如果您在 products.h 中使用字符串,您可能应该在其中包含 etc。另外,我认为“使用命名空间 std”有点不受欢迎。

    【讨论】:

    • 没错,当你真正需要的只是头文件中的一个字符串时,污染全局命名空间通常是一种不好的形式。
    【解决方案4】:

    在您的包含文件中,您必须将字符串name_ 声明为std::string name_

    【讨论】:

      【解决方案5】:

      你需要:

      std::string name_;
      

      另外,这里似乎缺少分号:

      void setCost(float cost)
      {
          cost_=cost
      }
      

      【讨论】:

        【解决方案6】:

        您在此函数中缺少分号:

        void setCost(float cost)
        {
            cost_=cost
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多