2012-11-01 40 views
0

我不断收到一个错误:发现意外的文件结束,我完全失去了。我已经检查了咖喱括号和括号,我在课程结束时放了一个分号,我无法弄清楚它有什么问题。非常感谢。C++视觉工作室错误,即时困惑

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



class operations{ 
    void checkout(){ 
     cout << "Check out here!!"; 
    } 
} 
void main(){ 
    string item; 
    int choice; 

    cout << "What do you want to do? " << endl; 
    cout << "Press 1 for checking out " << endl; 
    cout << "Press 2 for stocking " << endl; 
    cout << "Press 3 looking at recipts " << endl; 
    cin >> choice; 
    cout << choice; 

    if(choice == 1){ 
     void checkout(); 
    } 
    /*ofstream myfile; 
    myfile.open("inventory.txt"); 

    if(myfile.is_open()){ 
     cout << "Enter a grocery item" << endl; 
     getline(cin,item); 
     myfile << item; 
    } 
    cout << "Your grocery item is " << item; 
    myfile.close(); 
    system("pause");*/ 
}; 
+0

您在main()函数的末尾放了一个分号,而不是在类声明的末尾。 – Wyzard

+1

此外,在main()中对'void checkout();的调用在语法上不是有效的。首先,因为在调用函数时不写入返回类型(在这种情况下是'void'),其次,因为'checkout()'是成员函数,而不是独立函数,所以必须调用它在一个作为'operations'类的实例的对象上。 – Wyzard

+0

我仍然得到error.it说它在39行,但我的程序只有38行 – user1504257

回答

0

这是你的代码,修正为我解释,你要执行什么。

#include<iostream> 
#include<fstream> 
#include<string> 

using namespace std; 

class operations 
{ 
    public:void checkout() 
    { 
     cout << "Check out here!!"; 
    } 
}; 

int main() 
{ 
    string item; 
    int choice; 
    operations op; 

    cout << "What do you want to do? " << endl; 
    cout << "Press 1 for checking out " << endl; 
    cout << "Press 2 for stocking " << endl; 
    cout << "Press 3 looking at recipts " << endl; 
    cin >> choice; 
    cout << choice; 

    if(choice == 1) 
    { 
     op.checkout(); 
    } 

    return 0; 
} 

首先,请注意类声明后需要分号和方法声明

秒钟后不需要,请注意void checkout()在你的代码不认为这是你在你的类中定义的方法,而是将而是声明一个简单地不执行任何操作的新方法。要调用正确的void checkout()你必须instatiate operations类型的对象,然后调用其方法与op.checkout()

最后,始终声明int main()并将return 0如果执行流到达你的程序结束正确。

作为一个方面说明,我可能不会在你的程序中使用的一类,但main()实施前简单地实现方法通讯员用户的选择

void checkout() 
{ 
    cout << "Check out here!!"; 
} 

,这样就可以与

简单地给他们打电话
checkout() 
+0

没有必要添加'return 0;'。 –

2
  1. 您的类定义需要一个尾随分号,而不是你的主要功能。

    class operations{ 
        void checkout(){ 
         cout << "Check out here!!"; 
        } 
    }; 
    
  2. “void main”是错误的。 main总是返回int

+0

虽然你是对的,'main'应该返回'int',VC++接受'void main' – BigBoss

+0

@BigBoss,但是它在技术上是未定义的行为。 – chris

+0

http://stackoverflow.com/questions/636829/difference-between-void-main-and-int-main – Joe

-2

我想你应该在你的文件的顶部添加此(使这个第一行):

#include "stdafx.h" 
+0

没有任何迹象表明预编译头文件与此有关。 –