C++ c++;类内结构中的向量保持不变

C++ c++;类内结构中的向量保持不变,c++,class,vector,C++,Class,Vector,非常感谢您抽出时间回答我的问题! 对不起,我的坏C++ + BBH我更习惯于嵌入C,对于我所有的大学校园项目。 好的,在此我将发布整个代码段,由于您的评论,它现在已经被修改了 一些答案: 问题是,当我不使用“strcmp”时,我的编译器会给我以下警告:“描述资源路径位置类型” 在未指定行为[-Waddress]tafel.cpp”中与字符串文字结果进行比较 它确实以一种奇怪的方式运行,当我输入“end”时,它不会停止while循环 我最初键入的内容(d):参见代码 我试图纠正所说的一切。但我确

非常感谢您抽出时间回答我的问题! 对不起,我的坏C++ + BBH我更习惯于嵌入C,对于我所有的大学校园项目。 好的,在此我将发布整个代码段,由于您的评论,它现在已经被修改了

一些答案:

  • 问题是,当我不使用“strcmp”时,我的编译器会给我以下警告:“描述资源路径位置类型” 在未指定行为[-Waddress]tafel.cpp”中与字符串文字结果进行比较 它确实以一种奇怪的方式运行,当我输入“end”时,它不会停止while循环 我最初键入的内容(d):参见代码

  • 我试图纠正所说的一切。但我确实需要tafel::nieuwebestelling()方法中的tafelnummer静态变量。因为我正在做一个“餐厅计划”。变量也表示表号。例如,如果表1点了5瓶啤酒。载体的含量为5倍啤酒。指数(tafelnummer)为1。我认为这是该计划的一个良好开端

问题是:

--->问题仍然是一样的:首先我去函数“nieuwe-bestalling”,它允许我填充向量。之后,我用函数toevoegen打印向量,但它仍然是空的

守则:

/* *塔菲尔 *创建日期:2014年7月18日 *作者:亚历克斯 */

#包括“./tafel.h”
#包括
#包括
#包括
塔菲尔::塔菲尔(){
//施工
}
void tafel::nieuwebestelling()
{
std::字符串besteld;
静态int-tafelnummer=1;
while(besteld!=“结束”)
{

std::cout不确定它是如何工作的(除了评论中指出的明显问题),但在我这方面它是工作的。代码如下:

#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>

using namespace std;

const int tafels=16;

class tafel
{
public:
    struct bestelling 
    {
        vector<string>gerechten;  // <-- the vector
    }bestellingen[tafels];

    tafel(){};
    void nieuwebestelling();
    void toevoegen();
    virtual ~tafel(){};
};

void tafel::nieuwebestelling()
{
    char besteld[20]={};
    static int tafelnummer=1;

    while((strcmp(besteld,"end"))!=0)
    {
        cout<<"YOUR ORDER: ";
        cin.getline(besteld,20);
        bestellingen[tafelnummer].gerechten.push_back(besteld);
    }
    cout<<"---------------------------"<<endl;
}

void tafel::toevoegen()
{

    int tafelnummer=1;
    cout<<"THE VECTOR:"<<endl;   //contains what you ordered previously.

    //DOESN T GET PRINTED    
    for(unsigned int i =0; i< bestellingen[tafelnummer].gerechten.size(); i++)
    {
        cout<< bestellingen[tafelnummer].gerechten[i]<<endl;
    }
    cout <<"end"<<endl<<endl; //is printed
}

int main()
{
    tafel foo;
    foo.nieuwebestelling(); 
    foo. toevoegen(); // it prints the vector
}
#包括
#包括
#包括
#包括
使用名称空间std;
常数int tafels=16;
塔菲尔级
{
公众:
结构贝斯特林
{

vectorgerechten;//首先,您可能希望在问题中发布一个可编译的示例代码

例如,在您发布的代码中,有一个原始的类C数组
struct bestelling
,它是
tafel
类的数据成员:
bestellingen[tafels]
。但是
tafels
在哪里定义

<> P>此外,你的代码质量很低,因为它有C-ISM和C++的混合。 例如,为什么要混合使用
std::vector
和原始类C数组

除非有一个很强的理由不这样做,否则你应该总是考虑<强> <代码> STD::vector <强>作为C++中的默认容器,而不是原始的C类数组。 <> P>此外,使用StrcMP>>()/Case>比较字符串,并将原始代码<代码> char < /Cord>数组存储字符串。这是坏C++。只考虑使用<代码> STD::String < /Cord>类,适当地重载<代码>运算符==/COD>比较,并且远好于原始C类<代码> char < /Cord>数组。< /P> 换句话说,我建议您将代码从“旧C样式”和C++的混合代码,使用纯代码C++,使用<代码> STD::vector < /COD>和<代码> STD::String 。 如果调用其

std::vector
方法,则
std::vector
会自动增长,因此不需要在
tafel::nieuwebestelling()
方法中使用
tafelnummer
静态
变量。只需使用
std::vector
及其
push\u back()
方法向向量添加新内容即可


此外,在头文件中,使用带有
std::
前缀的完全限定名,例如
std::vector
(而不是
vector
)。事实上,在头文件中使用命名空间std;
会污染全局命名空间,这是一种不好的做法。

作为旁注:
while((strcmp(besteld,“end”)!=0)
是UB,因为
besteled
没有分配任何内容。
besteld
可以存储5个字符。但是您可以在
cin.getline(besteld,20)
中请求最多20个字符。使用
std::string
代替字符数组。这样更安全、更灵活。
/*
 * tafel.h
 *
 *  Created on: 18 Jul 2014
 *      Author: alex
 */

#ifndef TAFEL_H_
#define TAFEL_H_

#define tafels 10

#include <iostream>
#include <vector>
#include <string>


class tafel {
public:
    static int i;
    struct bestelling  //elke tafel heeft een struct waarin alle gerechten enzo in staan. ide struct = ide tafel
    {
        std::vector<std::string>gerechten;
    }bestellingen[tafels];

    tafel();
    void nieuwebestelling();
    void toevoegen();
};


#endif /* TAFEL_H_ */
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>

using namespace std;

const int tafels=16;

class tafel
{
public:
    struct bestelling 
    {
        vector<string>gerechten;  // <-- the vector
    }bestellingen[tafels];

    tafel(){};
    void nieuwebestelling();
    void toevoegen();
    virtual ~tafel(){};
};

void tafel::nieuwebestelling()
{
    char besteld[20]={};
    static int tafelnummer=1;

    while((strcmp(besteld,"end"))!=0)
    {
        cout<<"YOUR ORDER: ";
        cin.getline(besteld,20);
        bestellingen[tafelnummer].gerechten.push_back(besteld);
    }
    cout<<"---------------------------"<<endl;
}

void tafel::toevoegen()
{

    int tafelnummer=1;
    cout<<"THE VECTOR:"<<endl;   //contains what you ordered previously.

    //DOESN T GET PRINTED    
    for(unsigned int i =0; i< bestellingen[tafelnummer].gerechten.size(); i++)
    {
        cout<< bestellingen[tafelnummer].gerechten[i]<<endl;
    }
    cout <<"end"<<endl<<endl; //is printed
}

int main()
{
    tafel foo;
    foo.nieuwebestelling(); 
    foo. toevoegen(); // it prints the vector
}