C++头和cpp文件fpermisive

C++ header and cpp file fpermisive

本文关键字:fpermisive 文件 cpp 头和 C++      更新时间:2023-10-16

我真的很缺时间,我不知道我做错了什么。

我试图将3个类对象链接在一起,但我总是出错,在互联网上找不到任何答案。

我想知道如何正确地制作.h和.cpp文件。

也许你们中的一些人会看到我做错了什么。顺便说一下,我想把代码分开。

第1版:这是我得到的错误,我的错误:在recette/ListRecettes.cpp:8:0:recette/ListRecettes.h:23:42:erreur:ISO C++禁止声明没有类型[-fpermission]的"ajouterRecette"

第2版:ISO C++禁止声明没有类型的"ajouterRecette"[-fpermission]同样的事情编辑3:对不起,评论中的文件名不正确。

这里是标题

/* 
 * File:   ListeRecettes .h
 * Author: Necro
 *
 * Created on November 17, 2015, 1:51 PM
 */
#ifndef LISTERECETTES_H
#define LISTERECETTES_H
#include <vector>  //for std::vector
#include "iostream"
#include "Recette.h"

class ListeRecettes {
private:
    std::vector<Recette*> recettes;
public:
    ListeRecettes();
    ListeRecettes(const ListeRecettes& orig);
    void ajouterRecette(const Recette& recette);
    void enleverRecette(const Recette& recette);
    void afficher();
    virtual ~ListeRecettes();
};
#endif  /* RECETTES_H */

这里是.cpp

/* 
 * File:   ListeRecettes .cpp
 * Author: Necro
 * 
 * Created on November 17, 2015, 1:51 PM
 */
#include "ListeRecettes.h"
#include <vector>  //for std::vector
#include "iostream"
ListeRecettes::ListeRecettes() {
}
ListeRecettes::ListeRecettes(const ListeRecettes& orig) {
}
ListeRecettes::ajouterRecette(const Recette& recette) {
    this->recettes.push_back(recette);
}
ListeRecettes::enleverRecette(const Recette& recette) {
    for (int i = 0; i < recettes.size(); i++) {
        if (recettes.at(i).getNom() == recette.getNom()) {
            recettes.erase(i);
        }
    }
}
ListeRecettes::afficher(){
    for(int i = 0 ; i < recettes.size(); i ++){
        std::cout << recettes.at(i).getNom();
    }
}
ListeRecettes::~ListeRecettes() {
}

第4版:Recette.h

/* 
 * File:   Recette.h
 * Author: Necro
 *
 * Created on November 17, 2015, 3:14 PM
 */
#ifndef RECETTE_H
#define RECETTE_H
#include <vector>  //for std::vector
#include "Ingredient.h"
#include "iostream"
using namespace std;
class Recette {
public:
    Recette();
    Recette(const Recette& orig);
    Recette(string& , Ingredient&);
    string getNom();
    Ingredient getIngredient();
    virtual ~Recette();
private:
    string nom;
    vector<Ingredient*> listeIngredients;
};
#endif  /* RECETTE_H */

Recette.cpp

/* 
 * File:   Recette.cpp
 * Author: Necro
 * 
 * Created on November 17, 2015, 3:14 PM
 */
#include "Recette.h"
#include "iostream"
using namespace std;
Recette::Recette() {
}
Recette::Recette(string& nom, Ingredient ingredient) {
    this->nom = nom;
    this->listeIngredients.push_back(ingredient);
}
Recette::Recette(const Recette& orig) {
}
Recette::getNom(){
    return this->nom;
}
Recette::getIngredient(){
    return this->listeIngredients;
}
Recette::~Recette() {
}

成分.h

/* 
 * File:   Ingredient.h
 * Author: Necro
 *
 * Created on November 17, 2015, 2:18 PM
 */
#ifndef INGREDIENT_H
#define INGREDIENT_H
#include <iostream>
using namespace std;
class Ingredient {

public:
    Ingredient();
    Ingredient(string&,int);
    Ingredient(const Ingredient& orig);
    void addIngredient(int quantite);
    void removeIngredient(int quantite);
    virtual ~Ingredient();
private:
    string nom;
    int quantite;
};

#endif  /* INGREDIENT_H */

入口.cpp

/* 
 * File:   Ingredient.cpp
 * Author: Necro
 * 
 * Created on November 17, 2015, 2:18 PM
 */
#include "Ingredient.h"
Ingredient::Ingredient() {
}
Ingredient::Ingredient(string &nom, int quantite) :
nom(nom), quantite(quantite) {
    this->nom = nom;
    this->quantite = quantite;
}
Ingredient::Ingredient(const Ingredient& orig) {
}
Ingredient::addIngredient(int quantite) {
    this->quantite += quantite;
}
Ingredient::removeIngredient(int quantite){
    this->quantite -= quantite;
}
Ingredient::~Ingredient() {
}

您的签名肯定是错误的。

除了构造函数和析构函数之外,您必须每隔定义一次返回类型。

这样的东西:

void ajouterRecette(const Recette& recette);
void enleverRecette(const Recette& recette);
void afficher();

然后相应地更新cpp。

然后你可以发布错误!