2014-12-06 160 views
2

無論我使用getline函數,我在「error:expected primary-expression before」,「token」在我的代碼中。 我看了一些在ifstreams上使用getline的人的例子,我想也許我必須在調用函數之前加上「std ::」(不確定會做什麼?),但這也不起作用。C++錯誤「'」標記之前的預期主表達式(在ifstream上使用getline?)

這是我的代碼的第一位;我假設我所做的任何錯誤都是一樣的。

#include <stdio.h> 
#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 

struct ListNode{ 
    string user; 
    string salt; 
    string encrypted; 
    string password; 
    ListNode* next; 
}; 


int main(){ 

    /* Initializes singly linked list to hold user data */ 
    ListNode *root; 
    ListNode *conductor; 
    root = new ListNode; 
    conductor = root; 

    string temp; 

    //creates and opens filestream to password file                  
    ifstream psswdFile ("example.txt"); 

    if(psswdFile.is_open()){ 
    //we assume there is at least one line, and initialize the head of             
    //the list                           
    std::getline(psswdFile&, (conductor->user)&, ':'); 
    std::getline(psswdFile&, (conductor->salt)&, ':'); 
    std::getline(psswdFile&, (conductor->encrypted)&); 
+0

'std :: getline(psswdFile&,(conductor-> user)&,':');'你能解釋一下你在做什麼嗎? – PaulMcKenzie 2014-12-06 00:51:38

+0

你不需要'&'在C++中通過引用傳遞參數。只要寫'std :: getline(pswdFile,[...]' – 2014-12-06 00:54:12

回答

1

它看起來像你誤會你傳遞給C++函數參數引用的方式。不同於通常需要您使用&運營商獲得的地址指針,即採取引用不需要你做任何事情的功能:只需通過一個可分配的表達式:

std::getline(psswdFile, conductor->user, ':'); 

編譯器有你的函數的前置聲明,所以它知道通過你引用的任何東西。

+1

真棒,它的工作!謝謝,我想我需要重讀那一章! – possiblyATurtle 2014-12-06 00:55:53

相關問題