用前向声明编译难度

时间:2015-01-30 02:01:30

标签: c++ compilation

我有头文件list.h和item.h,它有我的类声明。如果我没有在main中声明我的类,就像这样:

#include <iostream>
#include <fstream>
#include <ctype.h>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include "item.h"
#include "list.h"


int main()
{
    bool runAgain = true;

    do
    {
        int userMenuSelection;
        int userContinueSelection;
        bool printAll = true; //print list w/ totals
        std::string outputFileName;
        std::ofstream ofs; //to output files
        std::ifstream ifs; //to import files

        std::cout << "Welcome. Please Enter a Number." << std::endl;
        std::cout << "1 - Create (add) list" << std::endl;
        std::cout << "2 - Add item to list" << std::endl;
        std::cout << "3 - Remove item from list" << std::endl;
        std::cout << "4 - Edit item" << std::endl;
        std::cout << "5 - View list & total price" << std::endl;
        std::cout << "6 - Save list to a file" << std::endl;
        std::cout << "7-  Import list from file" << std::endl;
        std::cout << "8 - Quit program" << std::endl;

        if (isdigit(userMenuSelection) == 0)
        {
            std::cout << "Please enter a number!" << std::endl;
        }
        else
        {
            switch (userMenuSelection)
            {
            case 1:
                list.addList();
                break;
            case 2:
                list.printLists(printAll);
                break;
            case 3:
                list.addItem();
                break;
            case 4:
                list.removeItem();
                break;
            case 5:
                list.editItem();
                break;
            case 6:
                std::cout << "File will be imported from import.txt in the current directory." << std::endl;
                std::cout << "Import List files are formatted as such: " << std::endl;
                std::cout << "Itemname-Price-Quantity" << std::endl;
                std::cout << "Examples:" << std::endl;
                std::cout << "Egg Whites-1.99-20" << std::endl;
                std::cout << "Apples-0.25-10" << std::endl;
                ifs.open("import.txt");
                list.importList(ifs);
                break;
            case 7:
                if (list.isEmpty() == true)
                {
                    std::cout << "There are no lists to export because you have not added any yet!" << std::endl;
                    break;
                }
                else
                {
                    std::cout << "What would you like the new file to be called?" << std::endl;
                    std::cin >> outputFileName;
                    ofs.open((outputFileName + ".txt").c_str()); //create text file with user entered name
                    list.exportList(ofs);
                    std::cout << "New text file with name " << outputFileName << " created in directory." << std::endl;
                    break;
                }
            case 8:
                return 0;
            default:
                std::cout << "Enter valid integer from 1 to 8." << std::endl;
            }
        }

        std::cout << "Would you like to keep running the program?" << std::endl;
        std::cout << "Enter 1 for YES, 2 for NO" << std::endl;
        std::cin >> userContinueSelection;

        if (isdigit(userContinueSelection) == 0)
        {
            std::cout << "You did not enter a number." << std::endl;
            std::cout << "Program will continue to run." << std::endl;
        }
        else
        {
            switch (userContinueSelection)
            {
            case 1:
                break;
            case 2:
                runAgain = false;
                break;
            default:
                std::cout << "You didn't enter a valid number." << std::endl;
                std::cout << "Program will continue to run." << std::endl;
            }
        }
    } while (runAgain);

    return 0;
}

这是编译器提供的:

 20% make
g++ -std=c++0x -Wall -g main.cpp list.cpp item.cpp -o Assignment2
main.cpp: In function ‘int main()’:
main.cpp:77: error: expected unqualified-id before ‘.’ token
main.cpp:80: error: expected unqualified-id before ‘.’ token
main.cpp:83: error: expected unqualified-id before ‘.’ token
main.cpp:86: error: expected unqualified-id before ‘.’ token
main.cpp:89: error: expected unqualified-id before ‘.’ token
main.cpp:99: error: expected unqualified-id before ‘.’ token
main.cpp:102: error: expected primary-expression before ‘.’ token
main.cpp:112: error: expected unqualified-id before ‘.’ token
main.cpp:53: warning: unused variable ‘printAll’
make: *** [Assignment2] Error 1

如果我包含类声明:

#include <iostream>
#include <fstream>
#include <ctype.h>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include "item.h"
#include "list.h"

class item

class list


int main()
{

编译器给出了这个错误:

 21% make
g++ -std=c++0x -Wall -g main.cpp list.cpp item.cpp -o Assignment2
main.cpp:49: error: two or more data types in declaration of ‘main’
make: *** [Assignment2] Error 1
flip1 ~/ecampus162/Assignment2 22% 

最后,如果我在声明后加上分号:

include <iostream>
#include <fstream>
#include <ctype.h>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include "item.h"
#include "list.h"

class item;

class list;


int main()
{

这是编译器提供的内容:

22% make
g++ -std=c++0x -Wall -g main.cpp list.cpp item.cpp -o Assignment2
main.cpp: In function ‘int main()’:
main.cpp:81: error: expected unqualified-id before ‘.’ token
main.cpp:84: error: expected unqualified-id before ‘.’ token
main.cpp:87: error: expected unqualified-id before ‘.’ token
main.cpp:90: error: expected unqualified-id before ‘.’ token
main.cpp:93: error: expected unqualified-id before ‘.’ token
main.cpp:103: error: expected unqualified-id before ‘.’ token
main.cpp:106: error: expected primary-expression before ‘.’ token
main.cpp:116: error: expected unqualified-id before ‘.’ token
main.cpp:57: warning: unused variable ‘printAll’
make: *** [Assignment2] Error 1

关于我做错的任何想法?感谢。

0 个答案:

没有答案