我已經開始從SEE編程抽象(CS106B)。我很難開始分配任務。這是熱身練習的代碼。我已經諮詢了各種不同的解決方案,但他們都沒有工作,因此被迫在這裏發佈。錯誤C3861:'getLine':標識符未找到
#include <iostream>
#include <cstring>
#include "console.h"
using namespace std;
/* Constants */
const int HASH_SEED = 5381; /* Starting point for first cycle */
const int HASH_MULTIPLIER = 33; /* Multiplier for each cycle */
const int HASH_MASK = unsigned(-1) >> 1; /* All 1 bits except the sign */
/* Function prototypes */
int hashCode(string name);
/* Main program to test the hash function */
int main() {
string name = getLine("Please enter your name: ");
int code = hashCode(name);
cout << "The hash code for your name is " << code << "." << endl;
return 0;
}
/*
* Function: hash
* Usage: int code = hashCode(key);
* --------------------------------
* This function takes the key and uses it to derive a hash code,
* which is nonnegative integer related to the key by a deterministic
* function that distributes keys well across the space of integers.
* The general method is called linear congruence, which is also used
* in random-number generators. The specific algorithm used here is
* called djb2 after the initials of its inventor, Daniel J. Bernstein,
* Professor of Mathematics at the University of Illinois at Chicago.
*/
int hashCode(string str) {
unsigned hash = HASH_SEED;
int nchars = str.length();
for (int i = 0; i < nchars; i++) {
hash = HASH_MULTIPLIER * hash + str[i];
}
return (hash & HASH_MASK);
}
編譯日誌:
所有的> 1>------ Build started: Project: Warmup, Configuration: Debug Win32 ------
1>Compiling...
1>Warmup.cpp
1>c:\users\users\google drive\courses\programming abstractions (stanford cs106b) 2012\assignments\assignment1-vs2008\assignment1-vs2008\0-warmup\src\warmup.cpp(30) : error C3861: 'getLine': identifier not found
1>Build log was saved at "file://c:\Users\Users\Google Drive\Courses\Programming Abstractions (Stanford CS106B) 2012\Assignments\Assignment1-vs2008\Assignment1-vs2008\0-Warmup\Warmup\Debug\BuildLog.htm"
1>Warmup - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
'getLine()'不是標準的C或C++函數。在C++標準庫和POSIX C庫中有各種形式的getline()。 C是區分大小寫的語言; C++也是如此。此外,使用C和C++進行雙重標記會導致憤怒(試圖響應的部分)或混淆(試圖理解的部分),因爲它們是非常不同的語言。 –
谷歌搜索「C++ getLine」直接導致我http://en.cppreference.com/w/cpp/string/basic_string/getline – chris
@chris'getline'不是'getLine' ... –