2017-10-13 139 views
1

所以我正在学习如何输出到文件,但插入运算符“< <”未被识别。C++ - 错误C2784: - 请参阅'std :: operator <<'的声明

因此,每当我在以下代码中使用“OutFile < <”时,Visual Studio将“< <”标记为无效。

我想也许这是一个与我的包含语句的问题,但我加倍检查,它似乎并不是我的情况。

完整的错误

"1>z:\cst 113 - programming\purdyjex4\purdyjex4.cpp(111): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'std::ifstream' 
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\string(167): note: see declaration of 'std::operator <<'" 

我的代码:

//Include - to use input & output on the screen 
#include <iostream> 

//Include - For file output and input 
#include <fstream> 

//Include - to use input and Output Manipulators 
#include <iomanip> 

//Include - to use string variables 
#include <string> 

//namespace using statement must be included to use the standard header files 
using namespace std; 

//Constant for programmer's name 
const string MY_NAME = "Justin T Purdy"; 

//Constant for Course & Exercise Number 
const string COURSE = "CST 113 - Exercise 4"; 

//To make outputting headers easier, store character and width for deviders 
const int SCREEN_WIDTH = 79; 
const char SYMBOL = '*'; 

//Cost Ratios 
const double DAY_COST = 0.1; 
const double NIGHT_COST = 0.25; 

//Name of files user can open 
const string FILE_NAMES[2] = { "Ex4-1.txt","Ex4-2.txt" }; 


int main(void) 
{ 
    //Declare variables; Step 1 
    ifstream inFile; 
    ifstream outFile; 

    //Create variables to be used 
    //I'm using arrays for some variables 
    string customerFirstName[3]; 
    string customerLastName[3]; 
    string customerFullName[3]; 
    int fileNumber; 
    int dayMinutes[3]; 
    int nightMinutes[3]; 
    double totalCosts[3] = { 0 }; 
    double grandTotal = 0; 

    //Output deviders 
    cout << setfill(SYMBOL) << setw(SCREEN_WIDTH) << SYMBOL << setfill(' ') 
     << endl; 

    //Get file number from user 
    cout << "Select from these two file names: " << endl 
     << "  1: Ex4-1.txt" << endl << "  2: Ex4-2.txt" << endl; 
    cout << setfill(SYMBOL) << setw(SCREEN_WIDTH) << SYMBOL << setfill(' ') 
     << endl; 
    cout << "Enter the number of the input file: "; 
    cin >> fileNumber; 

    //Check if invalid number, terminate program if number invalid 
    if (fileNumber > 2) 
    { 
     cout << endl << "You have entered a invalid fileNumber and the program terminated."; 
     return 0; 
    } 

    //Open file the user specified 
    inFile.open(FILE_NAMES[fileNumber]); 
    outFile.open("Ex4-Out.txt"); 

    //Get data from opened file, put it into variables 
    inFile >> customerFirstName[1] >> customerLastName[1] >> dayMinutes[1] >> nightMinutes[1] 
     >> customerFirstName[2] >> customerLastName[2] >> dayMinutes[2] >> nightMinutes[2] 
     >> customerFirstName[3] >> customerLastName[3] >> dayMinutes[3] >> nightMinutes[3]; 


    //Calculation total costs using data from file 
    totalCosts[1] = (dayMinutes[1] * DAY_COST) + (nightMinutes[1] * NIGHT_COST); 
    totalCosts[2] = (dayMinutes[2] * DAY_COST) + (nightMinutes[2] * NIGHT_COST); 
    totalCosts[3] = (dayMinutes[3] * DAY_COST) + (nightMinutes[3] * NIGHT_COST); 
    grandTotal = totalCosts[1] + totalCosts[2] + totalCosts[3]; 


    //Output header into output file 
    outFile << setfill(SYMBOL) << setw(SCREEN_WIDTH) << SYMBOL << setfill(' ') 
     << endl << MY_NAME << endl << COURSE << "CELL PHONE BILL" 
     << setfill(SYMBOL) << setw(SCREEN_WIDTH) << SYMBOL << setfill(' '); 

    //Output name of columns to output file 
    outFile << right << setw(10) << "Phone" << setw(20) << "Name" 
     << setw(15) << "Day" << setw(15) << "Night" << setw(15) 
     << "Total" << endl << setw(45) << "Minutes" << setw(15) 
     << "Minutes" << setw(15) << "Cost" << setfill(SYMBOL) << 
     setw(SCREEN_WIDTH) << SYMBOL << setfill(' '); 

    //Output first row of data 
    outFile << right << setw(10) << "1" << setw(20) << customerFirstName[1] 
     << setw(15) << dayMinutes[1] << setw(15) << nightMinutes[1] << setw(15) 
     << totalCosts[1] << endl; 

    //Output second row of data 
    outFile << right << setw(10) << "2" << setw(20) << customerFirstName[2] 
     << setw(15) << dayMinutes[2] << setw(15) << nightMinutes[2] << setw(15) 
     << totalCosts[2] << endl; 

    //Output third row of data 
    outFile << right << setw(10) << "3" << setw(20) << customerFirstName[3] 
     << setw(15) << dayMinutes[3] << setw(15) << nightMinutes[3] << setw(15) 
     << totalCosts[3] << endl; 

    //Output grand total 
    outFile << setfill(SYMBOL) << setw(SCREEN_WIDTH) << SYMBOL << setfill(' ') 
     << left << "Total family cost:" << right << grandTotal; 

    //Close file 
    inFile.close(); 
    outFile.close(); 

    return 0; 
} 
+2

你能缩小你的代码到更小的东西中吗? – Steve

+1

您将'outFile'定义为'ifstream outFile;'。您无法输出到输入流。你需要'ofstream outFile;'投票结束作为错字 – NathanOliver

+0

'ifstream'中的“i”代表“输入”。它无法处理输出。 –

回答

0

ifstream outFile;应该是ofstream outFile;。否则,你会写入一个输入文件,但没有定义。

相关问题