【问题标题】:What can I do to get rid of this syntax error on using namespace?我可以做些什么来摆脱使用命名空间的这个语法错误?
【发布时间】:2020-06-14 22:17:12
【问题描述】:

我知道每个人都会说不要在所有东西上使用命名空间,但我不知道我需要做什么才能摆脱它。

我正在尝试创建一个简单的程序,它包含三个属于一个类的项目,并为其中两个函数获取用户输入。

标题:

#include <iostream>
#include <string>
using namespace std;

class retailItem {
private:
    string description;
    int unitsOnHand;
    double price;
public:
    retailItem();
    retailItem(string, int, double);
    ~retailItem();
    void setDescription(string);
    string getDescription();
    void setUnitsOnHand(int);
    int getUnitsOnHand();
    void setPrice(double);
    double getPrice();
}

implementation.cpp:

#include "items.h"
using namespace std;

retailItem::retailItem() {
    description = "Small Tent";
    unitsOnHand = 200;
    price = 35.99;
}
retailItem::retailItem(string d, int u, double p) {
    description = d;
    unitsOnHand = u;
    price = p;
}
retailItem::~retailItem() {}
void retailItem::setDescription(string d) {
    description = d;
}
string retailItem::getDescription() {
    return description;
}
void retailItem::setUnitsOnHand(int u) {
    unitsOnHand = u;
}
int retailItem::getUnitsOnHand() {
    return unitsOnHand;
}
void retailItem::setPrice(double p) {
    price = p;
}
double retailItem::getPrice() {
    return price;
}

主要:

#include "items.h"
using namespace std;

void PrintItem(retailItem);
int TotalAmount(retailItem, retailItem, retailItem);
double TotalCost(retailItem, retailItem, retailItem);

int main() {
    string d;
    int u;
    double p;
    retailItem r1("Small Tent", 200, 35.99);
    retailItem r2;
    retailItem r3;

    cout << "Welcome to the tent store!\n\n";
    cout << "=====================\n\n";
    cout << "Enter description for item two: \n";
    getline(cin, d);
    cout << "Enter amount on hand for " << d << endl;
    cin >> u;
    if (u < 0)
        cout << "Amount must be greater than 0 \n";
    else;
    cout << "Enter price for " << d << endl;
    cin >> p;
    if (p < 0)
        cout << "Price must be greater than 0 \n";
    else;
    r2.setDescription(d);
    r2.setUnitsOnHand(u);
    r2.setPrice(p);
    cout << "Enter description for item three: \n";
    getline(cin, d);
    cout << "Enter amount on hand for " << d << endl;
    cin >> u;
    if (u < 0)
        cout << "Amount must be greater than 0 \n";
    else;
    cout << "Enter price for " << d << endl;
    cin >> p;
    if (p < 0)
        cout << "Price must be greater than 0 \n";
    else;
    r2.setDescription(d);
    r2.setUnitsOnHand(u);
    r2.setPrice(p);
    PrintItem(r1);
    PrintItem(r1);
    PrintItem(r1);
    cout << "\n The total amount on hand is: " << TotalAmount(r1, r2, r3) << endl;
    cout << "\n The total price of all items is: " << TotalCost(r1, r2, r3) << endl;
    }
void PrintItem(retailItem r) {
    cout << "\n Item Description: " << r.getDescription();
    cout << "\n Amount on hand: " << r.getUnitsOnHand();
    cout << "\n Item Description: " << r.getPrice();
}
int TotalAmount(retailItem r1, retailItem r2, retailItem r3) {
    return r1.getUnitsOnHand() + r2.getUnitsOnHand() + r3.getUnitsOnHand();
}
double TotalCost(retailItem r1, retailItem r2, retailItem r3) {
    return r1.getPrice() + r2.getPrice() + r3.getPrice();
}

enter image description here

【问题讨论】:

    标签: c++ visual-studio class


    【解决方案1】:
    class retailItem {
    
        // ... class declaration
    }
    

    您的类声明在此处缺少尾随分号。

    #include "items.h"
    using namespace std;
    

    #include 语句相当于将包含文件的内容直接插入到包含文件中,逐字。因此,您要求编译器有效地编译如下内容:

    class retailItem {
    
    // ... your class declaration.
    
    } using namespace std;
    

    这当然是 gobbledygook,因此会出现编译错误。只需添加一个尾随分号:

    class retailItem {
    
        // ... class declaration
    };
    

    【讨论】:

    • 那行得通。现在我可以清理它并修复现在出现的其他问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    相关资源
    最近更新 更多