Learning C++ online with SEE CS106B -- Compile errors
Posted: 2008-12-28 07:15pm
Alright,so I'm trying to learn more C++ via Stanford's SEE online courses.
Specificaly, I am working on CS106B -- Programming Abstractions.
I downloaded the course materials, including the assignments and stuff. But it didn't have any of the ".h" libraries that were called by their program. I found them on the class website, but they were packaged for Apple's Xcode and Microsoft's Visual Studio. A friend of mine was kind enough to unpack them into the bare .h files from Xcode and email them to me.
My programing experience is limited to a course in C++ that did not include anything about multi-file programs. We barely covered functions.
So I tried the warmup assignment for the course, and it will not compile.
When I try to compile (using the command below -- I'm using Linux)
I get:
(If I compile without the flags for g++ I don't get the warning about signed and unsigned integers)
If you ask me, it looks like the header file "simpio.h" never actually defines GetLine() (it just states that it exists) but can someone with more experience point out what the problem is? (even better, how to fix it!)
assign1warmup.cpp
genlib.h
simpio.h
Specificaly, I am working on CS106B -- Programming Abstractions.
I downloaded the course materials, including the assignments and stuff. But it didn't have any of the ".h" libraries that were called by their program. I found them on the class website, but they were packaged for Apple's Xcode and Microsoft's Visual Studio. A friend of mine was kind enough to unpack them into the bare .h files from Xcode and email them to me.
My programing experience is limited to a course in C++ that did not include anything about multi-file programs. We barely covered functions.
So I tried the warmup assignment for the course, and it will not compile.
When I try to compile (using the command below -- I'm using Linux)
Code: Select all
$ g++ -Wall -W -s -pedantic-errors assign1warmup.cpp
Code: Select all
assign1warmup.cpp: In function ‘int Hash(std::string, int)’:
assign1warmup.cpp:37: warning: comparison between signed and unsigned integer expressions
/tmp/ccvo6keq.o: In function `main':
assign1warmup.cpp:(.text+0x100): undefined reference to `GetLine()'
collect2: ld returned 1 exit status
If you ask me, it looks like the header file "simpio.h" never actually defines GetLine() (it just states that it exists) but can someone with more experience point out what the problem is? (even better, how to fix it!)
assign1warmup.cpp
Code: Select all
/*
* Program: warmup.cpp
* --------------------
* Program used to generate a hash code based on user's name.
* As given, this code has two compiler errors you need to track down
* and fix in order to get the program up and running.
*
* jzelenski Thu Apr 1 12:27:53 PST 2004
*/
#include <iostream>
#include "genlib.h"
#include "simpio.h"
using namespace std;
#define MAX_HASH_CODE 10000 // Upper bound for hash codes generated by program
/* Function prototypes */
int Hash(string s, int maxCode);
/*
* Function: Hash
* Usage: hash_number = Hash(key, max_hash);
* -----------------------------------------
* This function takes the key and uses it to derive a hash code,
* which is an integer in the range [0, maxCode - 1]. The hash
* code is computed using a method called linear congruence.
*/
#define Multiplier -1664117991L // Multiplier used in Hash function
int Hash(string s, int maxCode)
{
unsigned long hashcode = 0;
for (int i = 0; i < s.length(); i++)
hashcode = hashcode * Multiplier + s[i];
return (hashcode % maxCode);
}
int main ()
{
cout << "Please enter your name: ";
string name = GetLine();
int hashcode = Hash(name, MAX_HASH_CODE);
cout << "The hash code for your name is " << hashcode << "." <<endl;
return 0;
}
Code: Select all
#ifndef GENLIB_H_
#define GENLIB_H_
/*
* File: genlib.h
* Version: 1.0CPP
* Last modified on Wed Sep 18 13:41:31 PDT 2002 by jzelenski
* ----------------------------------------------------------
* This header file is indicated to be included in
* all the programs written for CS106B/X and provides a few
* common definitions. Note this header has a "using namespace std"
* clause. If a file includes this header, it can then use
* features from the std namespace without qualifying by scope.
*/
#include <string>
using namespace std;
/*
* Function: Error
* Usage: Error(msg)
* ------------------
* Error outputs an error string to the cerr stream and
* then throws a string exception corresponding to the error.
*/
void Error(string str);
/*
* Function macro: main
* --------------------
* The purpose of this macro definition is to rename the student
* main to Main in order to allow a custom main defined in our
* libraries to configure the application before passing control
* back to the student program.
*/
#ifdef __APPLE__
#define main Main
#endif
#endif /*GENLIB_H_*/
Code: Select all
#ifndef SIMPIO_H_
#define SIMPIO_H_
/*
* File: simpio.h
* Version: 1.0CPP
* Last modified on Wed Sep 18 13:34:29 PDT 2002 by jzelenski
* ----------------------------------------------------------
* This interface provides access to a simple package of
* functions that simplify the reading of console input.
*/
#include "genlib.h"
/*
* Function: GetInteger
* Usage: n = GetInteger();
* ------------------------
* GetInteger reads a line of text from standard input and scans
* it as an integer. The integer value is returned. If an
* integer cannot be scanned or if more characters follow the
* number, the user is given a chance to retry.
*/
int GetInteger();
/*
* Function: GetLong
* Usage: n = GetLong();
* ---------------------
* GetLong reads a line of text from standard input and scans
* it into a long integer. The long is returned. If the
* number cannot be scanned or if extra characters follow it,
* the user is given a chance to retry.
*/
long GetLong();
/*
* Function: GetReal
* Usage: x = GetReal();
* ---------------------
* GetReal reads a line of text from standard input and scans
* it as a double. If the number cannot be scanned or if extra
* characters follow after the number ends, the user is given
* a chance to reenter the value.
*/
double GetReal();
/*
* Function: GetLine
* Usage: s = GetLine();
* ---------------------
* GetLine reads a line of text from standard input and returns
* the line as a string. The newline character that terminates
* the input is not stored as part of the string that is returned.
*/
string GetLine();
#endif /*SIMPIO_H_*/