Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 没有印刷品;理论上,我需要一个主管道还是应该在没有它的情况下工作?_C++_Constructor_Printf_Composition - Fatal编程技术网

C++ 没有印刷品;理论上,我需要一个主管道还是应该在没有它的情况下工作?

C++ 没有印刷品;理论上,我需要一个主管道还是应该在没有它的情况下工作?,c++,constructor,printf,composition,C++,Constructor,Printf,Composition,我不想问这个,但我已经试了几个小时了,我想不出来。我对C++是全新的,无法理解为什么SpaFFFS不会在最后或两者都把任何东西放在外面。基本上,在VisualStudio2019中,除了弹出窗口外,什么都不会发生。我知道这是一个简单的解决方案,但我正在疯狂地试图找出它。另外,我是否必须有一个主要的还是应该工作没有它?好的,还有,我的构造函数看起来还好吗?我有一个绿色的弯曲在它下面,不知道如何修复它,或者如果我可以忽略它。我感谢所有能得到的帮助!谢谢大家! //#include "std

我不想问这个,但我已经试了几个小时了,我想不出来。我对C++是全新的,无法理解为什么SpaFFFS不会在最后或两者都把任何东西放在外面。基本上,在VisualStudio2019中,除了弹出窗口外,什么都不会发生。我知道这是一个简单的解决方案,但我正在疯狂地试图找出它。另外,我是否必须有一个主要的还是应该工作没有它?好的,还有,我的构造函数看起来还好吗?我有一个绿色的弯曲在它下面,不知道如何修复它,或者如果我可以忽略它。我感谢所有能得到的帮助!谢谢大家!

//#include "stdafx.h" - commented out as I think this version of VS does 
//this automatically (I looked under precompiled headers and it was listed as "Precompiled Header File"
//and it wouldn't work unless I commented it out

#include <iostream>

using namespace std;

// Base Entree class
class Entree
{
protected:
    char _entree[10];
public:
    const char* getEntree()
    {
        return _entree;
    }
};


// Base Side class
class Side
{
protected:
    char _side[10];
public:
    char* getSide()
    {
        return _side;
    }
};

class Drink
{
protected:
    char _drink[10];
public:
    Drink()
    {
        cout << "\n Fill cup with soda" << endl;
        strcpy_s(_drink, "soda");
    }
    char* getDrink()
    {
        return _drink;
    }
};

// ADDED CODE:

class ComboMeal
{
private:
    Entree* entree;
    Side* side;
    Drink* drink;
    char _bag[100];

public:
    ComboMeal(const char* type)
        {
        sprintf_s(_bag, "/n %s meal combo: ", type);
        }
        void setEntree(Entree * e)
        {
            entree = e;
        }
        void setSide(Side * s)
        {
            side = s;
        }
        void setDrink(Drink * d)
        {
            drink = d;
        }
        const char* openMealBag()
        {
            sprintf_s(_bag, "%s, %s, %s, %s", _bag, entree->getEntree(), side->getSide(), drink->getDrink());
            return _bag;
        }
};
int main()
{   
}

正如评论中所说,C++中执行的代码是main函数中的一个。构造函数是在创建相应类的对象时调用的,因此您至少应该有如下内容:

int main(){
    ComboMeal combo("my type one");
    return 0;
}
一个更完整的例子是:

int main(){
    ComboMeal combo("my type one");
    combo.setEntree(new Entree);
    combo.setSide(new Side);
    combo.setDrink(new Drink);
    cout << combo.openMealBag() << endl;
    return 0;
}

当然,这会打印垃圾,因为新对象中的值没有设置

如果主函数不执行任何操作,则不会发生任何事情。。。您可以拥有任意数量的函数和类,但如果没有代码使用它们,您将看不到太多!换句话说,编译器不知道你想用这些类做什么,除非你给它一些指令。是的,在C++中定义一个主函数是必要的。这是在程序中实现入口点的指定方式,没有入口点,代码的执行就无法开始。如果主函数什么都不做,那么就没有什么神奇的方法来实例化对象或调用成员函数。可以定义类的静态实例,这可能导致构造函数在main之前被调用,但这只在特定情况下完成,而您还没有这样做。谢谢!在阅读了上面的内容后,我意识到我确实需要使用main,不管geekforgeks怎么说。所以我现在正在编写代码。谢谢你!