2015-11-18 69 views
-3

我真的很短時間,我無法弄清楚我在這裏做錯了什麼。C++頭文件和cpp文件fpermisive

我想鏈接3類對象在一起,但我不斷收到錯誤的一切,我無法在互聯網上找到任何答案。

我想知道如何正確生成.h和.cpp文件。

也許你們中的一些人會看到我做錯了什麼。我希望通過這種方式分開代碼。

EDIT1: 這是錯誤我得到的,我的壞:8:在從抗皺/ ListeRecettes.cpp包含的文件0: 抗皺/ ListeRecettes.h:23:42:ERREUR:ISO C++禁止的「聲明ajouterRecette」無類型[-fpermissive]

EDIT2:ISO C++禁止的宣言 'ajouterRecette' 無類型[-fpermissive] 同樣的事情 EDIT3:在評論對不起文件名,其中不正確的。

這裏頭

/* 
* 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() { 
} 

Edit4: 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() { 
} 

Ingredient.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 */ 

Ingredient.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() { 
} 
+1

您不斷收到* what *錯誤? –

+0

我做了編輯對不起, –

+1

爲什麼在它本身包含'Recette.h'? – Azad

回答

1

你的簽名是絕對錯誤的。

你必須定義返回類型,除了構造函數和析構函數。

所以是這樣的:

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

然後相應地更新CPP。

然後你可以發佈錯誤!

+0

它做同樣的事情:S –

+1

相應地更新cpp。將「void」添加到cpp中的方法實現的開頭。 – Darryl

+0

謝謝我現在有一些錯誤來糾正。 –