CS252 : Frequently Asked Questions

Steven J. Zeil

Last modified: Dec 20, 2023
Contents:

This is a collection of questions (and answers!) that have arisen repeatedly in some of my past classes.

1 General CS252 Questions

1.1 Where can I get help on this course?

  1. You can ask the instructor questions about general course material and about the assignments by email.

  2. You can make use of the instructors’ office hours. See the syllabus for details.

  3. Tutoring is available from the Computer Science Tutoring Center for both walk-in and by-Zoom visits.

1.2 How can I search quickly through the lecture notes?

Use the “Search” button on the Outline, Resources, and other directory pages on the course website.

1.3 “…you have not done the Try This exercises…”

As noted early in the course, first in Working from a Command Line, then The Unix File System, and yet again in Basic File Manipulation, you saw notices like this:

Throughout this course you will encounter sections labeled “Try This:”. That means that I really want you to log in and try the commands or procedures I describe, and observe and think about the results.

Some of the “Try This:” exercises will set up files and directories that will be used in later exercises, so skipping these early examples may lead to problems later on. Some of the assignments will also check to be sure that you have done the Try This exercises before attempting the assignment.

You got this message because an assignment grading script detected that you had not done one or more of the Try Exercises. You will need to go back and do them.

In some cases, you may have done the Try This exercise but done them incorrectly. For example, if the exercise told you to create a directory ~/playing and you instead created a directory ~/Playing or ~/playng or ~/UnixCourse/playing, the grading script would conclude that you had not completed the exercise. There is simply no way that an automated program can account for the literally infinite number of possible misspellings and misplacings that people could possibly commit.

2 Connection and Login Issues

2.1 I can’t log in to CS Dept SSH servers

Try logging in to this page with your CS network credentials.

If none of the above suggestions help, you will probably need to contact root@cs.odu.edu for assistance. Be sure to tell them whether you were able to use your account to log into the web page linked above.

2.2 I can’t log in to the CS VPN

Try logging in to this page with your CS network credentials.

2.3 I can’t log in to CS252 web pages

Try logging in to this page with your CS network credentials.

2.4 “unable to connect, host does not exist”

You need to run the VPN before making your SSH connection.

2.5 “The authenticity of host … can’t be established.”

This is a normal and expected message whenever you connect to a new machine for the first time.

Just answer “yes” the to question about whether to continue connecting.

2.6 “POSSIBLE DNS SPOOFING DETECTED” or “REMOTE HOST IDENTIFICATION HAS CHANGED”

This message arises whenever the “hardware” that responds to connections to a particular machine name (e.g., linux.cs.odu.edu) has changed. Your SSH client remembers the hardware IDs of your past connections and objects when the hardware appears to have changed.

This practice was instituted for SSH a long time ago and made sense at the time as protection against “DSN spoofing” (a.k.a.“man-in-the-middle”) attacks where someone on the network would insert their machine as a kind of wire-tap between you and your intended destination in order to spy out your login name and password.

It’s now pretty much just an annoyance because

  1. DNS spoofing is almost impossible if you are connecting via the VPN.
  2. In the modern world of cloud computing, the servers that respond to your connection attempts probably aren’t hardware at all, but “virtual machines” simulating the hardware, and those virtual machines may change at any time, far more frequently than anyone would ever purchase new hardware.

So what do you do?

2.7 “Please contact your administrator or connect to EMS for license activation”

This message from the VPN client indicates that you installed the wrong version of the VPN software –– you have the free trial of the commercial package instead of the free open-source version.

Try uninstalling it and then reinstall. This time, when you visit the company page for the download, make sure that you go all the way to the bottom of the page and get the “Forticlient VPN”, not the full “Forticlient” package.

2.8 ssh-add on Windows 10 says: communication with agent failed

  1. Make sure that your agent is actually running.

    • In the taskbar search area, type “services”. Select the “Run as administrator” option.
    • Find the “OpenSSH Authentication Agent” service. Does it say that it is running? If not, start it. If it was running, proceed to the next step.
  2. Some (older) builds of Windows 10 are missing a required component of the Agent service. That component is, however, in the SSH server optional feature.

    1. Open Settings.
    2. Go to Apps, then Apps and Feature, then Optional Features.
    3. Find the entry for OpenSSH Server, click on it, and then click Install.

    Follow the on-screen instructions to complete the installation. 4. Leave this service on “Manual” so that it does not start automatically. You don’t actually want to run this service. You just need it to install the missing component.

    Try ssh-add again.

3 Common Unix Command Errors

3.1 “Permission denied”

In Unix, there are three basic kinds of permissions on every file:

  1. Read permission: allows you (or the commands that you issue) to look at the contents of that file.

  2. Write permission: allows you to alter the contents of a file.

  3. Execute permission: allows you to execute or run the file as if it were a command or program.

(These same permissions also apply to directories, but the meanings are slightly different.)

So, in general, if you get a Permission denied error, it’s because you tried to read/write/execute a file for which you did not have the corresponding permission.

In CS252, there seem to be two very common reasons why people get this.

Common Reason 1: That’s not YOUR file

Many CS252 students will mistakenly try to create or edit files in one of my directories, e.g.:

cp foo.dat ~cs252/Assignments/wherever/

~cs252 is shorthand for /home/cs252, the home for the cs252 class account, which is my directory, not yours.

It should not surprise you that I don’t give you write permission on my files and directories.

Common Reason 2: That’s not a program or command

For ordinary data files that you create and work with, you will have read and write permission, but not execute permission, because you can’t execute ordinary data files. They’re data, not programs.

All Unix commands start with a command or program name, followed by zero or more parameters, most of which are going to be paths to files. Examples are:

cp /usr/include/math.h /usr/include/stdio.h ~/playing
mv ~/playing/math.h ~/playing/math0.h 
g++ -o newProgram newProgram.cpp
./newProgram

But sometimes CS252 students forget to put in the command or program:

~/playing/math.h ~/playing/math0.h 

Now, when I look at that, I wonder if the student wanted to copy the first file, rename it, delete the two files, open both files in an editor, or something else?

But when the command shell (the program that reads your typed commands and launches the actual program to carry out the command) looks at that, it assumes that you meant the first thing listed to be a command or program. So you wind up seeing something like:

~/playing/math.h ~/playing/math0.h 
Permission denied

Why? Because the command shell assumed that you wanted it to execute ~/playing/math.h, and that’s an ordinary data file, so you don’t have Execute permission on it.

See also: Try This (bad commands)

3.2 “No such file or directory”

This is actually pretty self-explanatory. You just issued a command containing a path to some file, and the path does not actually match any existing file.

Usually, this is just a matter of a simple spelling mistake. But it throws some people for a loop, because they just can’t seem to believe that they actually typed something wrong.

What can you do about it?

Look and see what the problem is.

ls is one of the first Unix commands that you learned. Use it!

Diagnosing the reason for this error message should almost always be possible just by using the ls command.

Avoid making the mistake in the first place.

No, I’m not being snarky. Remember that you can use the Tab key after typing a few characters of a file or directory name to ask the command shell to fill in as much of the remaining name as it can.

That’s a great way both to avoid misspelling things in the first place (because you aren’t typing as much) and also to catch misspellings as soon as you make them (because the command shell doesn’t find any valid file or directory names that start with the few letters you just typed).

A common problem responsible for many of these messages is not paying attention to the ways that we can abbreviate home directory paths.

There is a big difference between ~cs252/ and ~/cs252/

3.3 Where do I find the file ~cs252/Assignments/fileAsst/foo.txt (…or some other lengthy path)?

How heavy is a 5 pound bag of flour?

Also, see 2.2 “No such file or directory” for the use of the ls command to systematically search for files along a path.

3.4 “omitting directory”

This message comes from the cp command when you try to copy a directory instead of a file or group of files.

The cp command, under normal circumstances, copies only ordinary files and not directories. It is possible to change this behavior, but because copying entire directory structures is somewhat risky (you can waste a lot of storage with some simple mistakes), it’s turned off by default.

3.5 cp: missing destination file operand

Look at the basic definition of the cp command.

In it’s simplest form, it requires two pieces of information (command arguments). One is what you want to copy, the source. The other is where you want to copy it to, the destination. There are more elaborate forms of cp as well, in which you give a whole list of files to copy. But you still will always need to end a cp command with the destination.

Look now at all the uses of cp in the Try This exercises in that same lesson. (Just hit Ctrl-f and search the page for “cp”.) They all exhibit that pattern.

You’ve only given one piece of information in your command, the source. You are, as the error message states, missing the destination operand.

3.6 I get time-out errors when trying to connect to a server

Quite a few possibilities:

  1. You are trying to connect to the wrong machine, or requesting a service of a machine that does not provide that service.

    For example, if you try to make an ssh connection to linux, the connection will time out because “linux” is not the (full) name of a machine on the Internet. (“linux.cs.odu.eduis a proper name of a machine.)

    If you try to make an ssh connection to vcportal.cs.odu.edu, the request will time out because, although that machine exists, but it’s not an ssh server and isn’t even listening to the the ports used for incoming ssh connections.

  2. You aren’t waiting long enough. Look at the settings you are using in your client program for connecting to the machine. If you see a short time-out, you might want to try allowing more time.

    This seems to be a common problem with the sftp clients FileZilla and WinSCP, which by default time out after a few seconds. Often, that’s just not long enough.

  3. Your own network connection may be down. See if you can connect to other internet services such as your favorite websites.

  4. Try waiting a while and then connecting again. If ODU has suffered a power outage, we have backup power systems that usually cut in nearly immediately. Machines may need to reboot, but that’s a matter of minutes at most.

  5. ODU might have suffered a massive Internet disconnect. Try using a web browser to visit the CS Dept home page. If it’s unreachable, other services may be unreachable as well.

  6. See if you can read the ODU CS machines at all. Open a terminal window on your PC (Windows users: click in the search bar and type “cmd”). Give the command:

    ping -n 4 -w 10000 linux.cs.odu.edu
    

    This will check to see if you can reach the Linux machines at all and how long it takes. If the output does not indicate that you are receiving a reply, then there is a problem with the network connection between you and ODU. It might be at the ODU end. It might be at your end. it might be somewhere in between.

  7. The machine might be down. You can report this to root@cs.odu.edu.

  8. Do you have firewall software installed on your PC? If so, try disabling it temporarily and try connecting again. If that works, reconfigure the firewall to allow outgoing connections on port 22.

  9. You certainly will have a firewall built in to the router that connects you to your Internet Service Provider. If you know how to do so, check to see if it is blocking outgoing connections on port 22. (Note that blocking incoming connections is normal, but some people in a fit of over-zealousness block outgoing connections as well.)

    A roundabout way to check this, if your PC is a laptop, is to use someone else’s WiFi. Visit a public library or school or a coffee shop that offers free WiFi and see if you can connect from the parking lot.

  10. Try connecting to the CS Dept VPN, and then try connecting to the CS Linux servers again.

  11. Finally, there is a possibility that your IP address has been blacklisted by the ODU CS security filters, presumably because of a pattern of network connection attempts that resembled attempted hacking. (This could be because your PC or some PC on your home network has been infected with malware that is attempting to spread itself. It is also true that most Internet Service Providers periodically reassign and recycle IP addresses, so your current IP address could be blacklisted because of someone else’s past misbehavior.)

    Look up your own IP address and email the CS systems staff at root@cs.odu.edu. Describe the problem to them. Tell them your CS login name, your IP address, what machine you are trying to connect to, and what ssh/sftp client you are using. Tell them about the steps you have performed above to diagnose the problem and what you have observed.

4 Managing Your Account Directories

4.1 I have files in my directories that I don’t own and can’t delete.

If an ls -l command reveals to you that you have some files or directories that belong to me, to ~cs252 or someone else in your account area, there’s a good chance that you won’t be able to delete them.

As far as I know, the only way this happens is because you tried to copy files using “cp -a”. This may be a good time to remind you that:

  • As a general rule, everything you need to solve the assignments will be something that is covered in my lecture notes and that I have asked you to practice with in the “Try This” exercises.
  • Avoid the temptation to go hunting up strange commands and command options on the internet or even in the Linux man pages in the belief that the magic answer is hidden out there somewhere.

The “-a” option of the “cp” command is definitely not something covered in the Lecture Notes nor that you have practiced with in the Try This exercises. It isn’t covered because it is rarely useful and often leads to exactly this sort of problem.

If you encounter this problem and can’t delete the errant files, the bad news is that “owner” of the files probably won’t be able to delete them either. That’s because although the owner likely has the permissions to manipulate those files, the owner probably does not have permission to navigate through your directories to where those files are located, and probably does not have permission to delete things from your directories either.

Here’s what you need to do:

  1. Use the mv to rename the file/directory to something that won’t interfere with your work, e.g., “garbage” or “deleteThis”.

  2. Send email to root@cs.odu.edu explaining the problem. Ask them to delete the file/directory. Make sure that you give them the absolute path to the renamed file or directory that you want removed.

A final word of advice. If you are asked to copy all files from a directory, that is not the same thing as copying the directory. As suggested elsewhere, this is like the difference between eating the cookies in a box versus chewing on the cardboard box itself. To copy the files from a directory, the command is

cp path-to-source-directory/* path-to-destination-directory/

The * is the wildcard pattern that matches any string of characters.

4.2 I deleted some files by accident. Can I get them back?

Files on our Linux servers are backed up every six hours. So if these files have existed for a while, you can recover them, although you may lose any changes you made since the last backup.

4.3 “Quota Exceeded”

If you get this message after running a command on one of our Linux servers, you need to reduce the amount of file storage you are using.

There’s a good chance that there are a relatively small number of files clogging up your account area. You may well discover that you don’t need them, or even know that you had them. Common problems are:

This command will list the 10 largest files in your account area:

find -H ~ -xdev -type f -printf '%s %p\n' | sort -nr | head -10

(The find command searches through your home directory printing all the files in the format “size filepath”, the sort command sorts by the file size (largest first), and the head command selects the first 10 lines. If you want a longer listing, change the “10” at the end to a larger number.)

This command is useful if you have a small number of very large files clogging up your account area.

What if, instead, you have a large number of relatively small files filling up your quota? This command will list the 10 largest files and directories in your account area:

du ~ -a | sort -nr | head -10

Not surprisingly, you will probably see your home directory listed first, because it contains everything else. Again, if you need a longer listing, change the “10” at the end to a larger number.

Once you have located the files that are giving you problems, use the rm command to remove them. If you are unable to do so, use “ls -l” to check your permissions on those files and change the permissions if necessary. If you still cannot remove the offending files, you may need to contact root@cs.odu.edu and ask them to do so.

4.4 “No space left on device”

This could be caused by your using more storage than is permitted for your account. It can also be caused by other people using up all available space on the disk.

Log in and give the command

df ~

This will print a report on available space on the drive where you account resides. If the “Available” number is near zero, report this to root@cs.odu.edu.

Otherwise, follow the suggestions in this question to trim your own use of files.

5 Git and SSH key issues

5.1 ssh complains about an “UNPROTECTED PRIVATE KEY FILE!”

Your private key file should be readable (and writable) by you alone.

5.2 SSH prompts me for a password instead of using my key

The most likely reasons for this are

  1. You have not properly authorized the public half of your key.
  2. You aren’t running an SSH agent on your own PC.
  3. You have not added the private half of your key to the agent.
  4. You did not use the “ssh -A” option to connect to the remote Linux machine, or you are in X2Go, which does not support that option.

Troubleshooting

On your local PC, give the command

ssh-add -l

If it complains that it can’t contact a server, then your problem is #2 above. Refer to Starting an Agent….

If it does not complain, but does not list your GitHub SSH key (or any key at all), then your problem is #3 above. refer to Adding Keys to the Agent.

If it lists your keys, then, in your SSH session to the remote Linux machine, give the same command

ssh-add -l

If it complains that it can’t contact a server or fails to list your key, then the problem is #4 above. Refer to Cloning an Existing Project.

If that command on the remote machine does list your key, then your problem is likely #1 above. Refer to Authorizing Keys.

5.3 Git prompts me for a password when I try to clone, push, or pull.

These three actions (clone, push, & pull) are the ones that communicate directly with GitHub. Other actions (e.g., add, commit) work on your local copy of the repository only.

You need to provide an SSH key to log into GitHub for those three actions. If you are prompted for a password, it’s a sign that your SSH key was not seen or was rejected.

The most likely reasons for this are

  1. You have not properly registered the public half of your key with GitHub.
  2. You aren’t running an SSH agent on your own PC.
  3. You have not added the private half of your key to the agent.
  4. You did not use the “ssh -A” option to connect to the remote Linux machine, or you are in X2Go, which does not support that option.

Troubleshooting

On your local PC, give the command

ssh-add -l

If it complains that it can’t contact a server, then your problem is #2 above. Refer to Starting an Agent….

If it does not complain, but does not list your GitHub SSH key (or any key at all), then your problem is #3 above. refer to Adding Keys to the Agent.

If it does list your keys, then open an SSH session to the remote Linux machine (or use the VSCode terminal if you are doing remote development with VSCode), and give the same command to the remote server:

ssh-add -l

If it complains that it can’t contact a server or fails to list your key, then the problem is #4 above. Refer to Cloning an Existing Project.

If that command on the remote machine does list your key, then your problem is likely #1 above. Refer to Logging in to GitHub.

5.4 Git tells me “Permission denied (public key).”

Either

Troubleshooting

On your local PC, give the command

ssh-add -l

If it complains that it can’t contact a server, then your problem is #2 above. Refer to Starting an Agent….

If it does not complain, but does not list your GitHub SSH key (or any key at all), then your problem is #3 above. refer to Adding Keys to the Agent.

If it does list your keys, then open an SSH session to the remote Linux machine (or use the VSCode terminal if you are doing remote development with VSCode), and give the same command to the remote server:

ssh-add -l

If it complains that it can’t contact a server or fails to list your key, then the problem is #4 above. Refer to Cloning an Existing Project.

If that command on the remote machine does list your key, then your problem is likely #1 above. Refer to Logging in to GitHub.

6 Eclipse & VSCode Issues

6.1 Eclipse says that your workspace is currently in use.

If you drop your connection to an X2Go session either accidentally (e.g., the network failed) or deliberately (you closed the connection without shutting down your running programs first), X2Go preserves your session. When you reconnect to that same server, X2Go will bring up the windows that you had open when you left.

If you get this message from Eclipse, you probably have an Eclipse session still running on a different server. Select “Choose” and then “Cancel” to get out of this. Then reconnect X2Go via the server you used earlier. If you don’t remember which server that was, you may need to try all of them until you find the one that pops up your old Eclipse window.

This is, by the way, one reason why I recommend configuring your X2Go client to go to specific servers instead of to the switcher linux.cs.odu.edu.

6.2 I created an Eclipse Java project with the wrong settings. How do I re-do it?

A lot depends on just how much is wrong. Minor problems can be fixed by right-clicking on the project name, selecting “Build path”, then “Configure Build Path” and adjusting your settings from there.

For major issues, it is often easier to to ask Eclipse to re-examine your project and deduce your settings, though you may still need to make minor tweaks to the build path afterwards.

To do this:

  1. In Eclipse, delete your project. Do not check the box to delete the project contents. You just want Eclipse to stop tracking the project.

  2. In a terminal session, cd to the project directory and locate and delete the .project and `.classpath`` files. These are where Eclipse stored your old project settings.

    • Take note that these file names start with “.”, which means that in Linux and MacOS these files are “hidden” by default. You can spot them by adding the “-a” to your “ls” commands.

  3. Back in Eclipse, create or import your project again.

6.3 How do I supply command line parameters when executing my programs?

Often you will be writing programs that take “command line parameters” as part of their inputs.

Of course, if you are executing a program from the command line, you just type the parameter values on the command line (hence the name!), e.g.,

java MyPackage.MyProgram ../testData/myTestData.txt 23

But what if you are launching your program from an IDE?

6.3.1 Eclipse

  1. After you have successfully compiled your code, right-click on the Java class that contains your main function. Then, from the Run menu, select Run configurations…

    You’ll see a list of different project types, one of which should be “Java Application”.

    • Underneath that, you might already have a configuration for this program (Clicking on a configuration will show you what project and what Java class it is associated with.)

    • If not, select “Java Application”, then at the top of the column click on the icon for “New launch configuration”.

  2. On the “Arguments” tab, enter your command line parameters into the “Program Arguments” box.

  3. Click Run to save these arguments and launch your program.

6.3.2 VSCode

The first time that you want to run a Java program with command line parameters requires some preparation.

  1. Open a Java file in the editor, preferably the one that contains the main function for the program that you want to run.

  2. From the Run menu, select Add Configuration...

    A file .vscode/launch.json will be created and opened in your editor. You should see something that looks like this:

    {
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "type": "java",
                "name": "Current File",
                "request": "launch",
                "mainClass": "${file}"
            },
            {
                "type": "java",
                "name": "MainProgram",
                "request": "launch",
                "mainClass": "cs252.MainProgram",
                "projectName": "mainprogram_528ad334"
            }
        ]
    }
    

    This creates two or more run configurations, the first one (highlighted above) to run whatever Java file you have open and the rest to run Java files that have a main function.

  3. Delete the highlighted option. It creates more confusion (IMO) than it’s worth.

  4. Go to the configuration that names the class with the main function that you want to run. Add a line of the form

    "args": [ list_of_command_line_parameters ],

    with each parameter in the list inside quote " " and separated from one another by commas.

    For example, if you want to run the equivalent of

    java cs252.MainProgram abc 123
    

    you should edit launch.json like this:

    {
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "type": "java",
                "name": "MainProgram",
                "request": "launch",
                "mainClass": "cs252.MainProgram",
                "args": ["abc", "123"],
                "projectName": "mainprogram_528ad334"
            }
        ]
    }
    

    Save your changes to .vscode/launch.json

  5. Now you should be able to run the program, with those parameters, either from the VSCode debugger or by right-clicking on the Java file and selecting Run Java.

6.4 Eclipse “New Project” does not show an option to create C++ projects

There is a bug in the current installation of Eclipse on our Linux servers that can cause this problem if you switch from one Linux server to another between Eclipse sessions.

  1. One fix is to simply reconnect to the same server that you used before, and to consistently use that server for working in Eclipse.
  2. Alternatively, close down eclipse, do
    rm -r ~/.eclipse
    

    and then restart Eclipse. If you have customized your Eclipse settings, you may lose your customizations by doing this.

  3. If neither of the above works for you, you can run my own installation of Eclipse via the command
    ~zeil/src/eclipse/eclipse &
    

6.5 Eclipse will not reopen C++ files, complains about missing cdt components.

There is a bug in the current installation of Eclipse on our Linux servers that can cause this problem if you switch from one Linux server to another between Eclipse sessions.

  1. One fix is to simply reconnect to the same server that you used before, and to consistently use that server for working in Eclipse.
  2. Alternatively, close down eclipse, do
    rm -r ~/.eclipse
    

    and then restart Eclipse. If you have customized your Eclipse settings, you may lose your customizations by doing this.

  3. If neither of the above works for you, you can run my own installation of Eclipse via the command
    ~zeil/src/eclipse/eclipse &
    

7 C++ & General Programming Questions

7.1 What’s all this “foo” and “bar” stuff?

There is a long-standing tradition in computer science of using certain words as sample variable/function/whatever names. Just as a mathematician might use “x” or “y” whenever an arbitrary variable name is needed, computer scientists tend to use “foo”, “bar”, and “baz”, in that order. Check out this entry in the Hacker’s Dictionary for a discussion of the origin of these terms.

7.2 Why do compilers’ error messages often give the wrong line number, or even the wrong file?

A compiler can only report where it detected a problem. Where you actually committed a mistake may be someplace entirely different.

Let’s look at a simple example:

7.2.1 Example 1

Assume that the compiler has read part, but not all, of your program. The part that has just been read contains a syntax error. For the sake of example, let’s say you wrote:

x = y + 2 * x // missing semi-colon

Now, when the compiler has read only the first line, it can’t tell that anything is wrong. That’s because it is still possible, as far as the compiler knows, that the next line of source code will start with a “;” or some other valid expression. So the compiler will never complain about this line.

If the compiler reads another line, and discovers that you had written:

x = y + 2 * x // missing semi-colon
++i;

The compiler knows that you did something wrong. But it still won’t conclude that there’s a missing semi-colon. For all it knows, the “real” mistake might be that you meant to type “+” instead of “++”.

7.2.2 Example 2

Now, things can be much worse. Suppose that inside a file foo.h you write

class Foo {
    ⋮
   Foo();
   int f();
   // missing };

and inside another file, bar.cpp, you write

#include "foo.h"

int g() { ... }

void h(Foo) { ... }

int main()  { ... }

Where will the error be reported? Probably on the very last line of bar.cpp! Why? Because until then, it’s still possible, as far as the compiler knows, for the missing “};” to come, in which case g, h, and main would just be additional member functions of the class Foo.

So, with most error messages, you know only that the real mistake occurred on the line reported or earlier, possibly even in an earlier-\#include’d file.

7.3 I’m getting “…undeclared…” or “No match for…” errors when I compile C++ code

In C++, most things that you give names to (e.g., variables, functions, etc.) need to be both declared and defined.

You declare something by introducing its name and sating what type of thing it is. For example:

int foo (int x); 

declares a function named “foo” and a parameter named “x”.

The rule in C++ is that you must declare names before your try to use them.

If you get a message that says that something is undefined or is not a match for the name appearing in some line of code, then you have either

7.4 I’m getting “undefined reference to…” errors when I compile C++ code

In C++, most things that you give names to (e.g., variables, functions, etc.) need to be both declared and defined.

Your define something by supplying its name, description, and the initial value, function body, or other information that “completes” everything the compiler needs to know about that named thing. For example:

int foo (int x)
{
    return x + 2;
}

defines the function named “foo”.

The rule in C++ is that you must define things exactly once in all of the compilation units (.cpp files) that make up your program before producing your final executable.

If you are getting a message saying that some name is undefined, it means that the compiler/linker could not find that definition when it tried to generate your final executable program.

Most often, this seems to happen with functions. The possible causes are:

8 Miscellaneous

8.1 How do I type the M- key in emacs?

This is covered in the emacs tutorial, but the basic answer is that it depends on what kind of PC you are one and on what ssh client or X server software you are using. But there are several options, and at least one of those is guaranteed to work:

"M-<chr>  means hold the META or EDIT or ALT key down while typing <chr>.
 If there is no META, EDIT or ALT key, instead press and release the
 ESC key and then type <chr>.  We write <ESC> for the ESC key."

Also take note that if you have OS/mode or Alt keys on both sides of the space bar, some ssh clients and X servers will treat them differently, often reserving one for issuing commands to your PC’s operating system while using the other to send things through the ssh connection to the remote machine. So you may need to try both the left and the right keys separately.

When in doubt, however, the Esc key always works. But It’s not a modifier like Shift or Ctrl that gets held down while you type the other keys. You type Esc and release it before giving the rest of the character sequence.

8.2 How do I examine the values of pointers in a debugger?

First, understand that a pointer is an address. It is usually printed as a hexadecimal (base 16) number. A common convention for displaying hexadecimal numbers is to print them immediately after “0x”, so, for example, 0x10A would denote the number 266 ($1 * 16^2 + 0 * 16 + 10$).

If a pointer value is shown as 0x0, it is null and doesn’t point to anything at all.

If a pointer has a non-zero, non-null value, you often want to see the data that it points to.

8.3 In regular expressions, how can I match a string of exactly K characters?

K might be $3$, $4$, $5$, or any fixed value.

  1. Write a pattern that matches at least K characters.
  2. Use ^ to anchor the first character of your pattern to the beginning of the string.
  3. Use ‘$’ to anchor the last character of your pattern to the end of the string.

Because if you match K characters somewhere in a string, and the first character you match is at the start of the string and the last character that you match is at the end of the string, then “obviously” the string can only be K characters long.

For example, if you wanted to match only strings of length 3 in which the first character is an upper-case letter and the last character is a lower-case letter, you could use the regular expression

^[A-Z].[a-z]$

because

Surprisingly, a lot of people seem to get the idea of using the ^ or the $, but don’t think to use both.

8.4 I cannot install software on my Mac: “developer cannot be verified”

That’s a common problem when using almost any open-source software on MacOS. The solution is covered in the macOS User Guide.

8.5 How do I unpack a Zip file on my own PC?

If you are running Windows…

Open the Windows File Explorer, navigate to the directory containing the Zip file, and double-click on the Zip file to open it. You can now copy-and-paste the files inside the Zip archive to any regular directory on your PC.

If you are running MacOS…

Open the Mac Finder, navigate to the directory containing the Zip file, and double-click on the Zip file to open it. You can now copy-and-paste the files inside the Zip archive to any regular directory on your PC.

If you are running Linux…

cd to the directory containing the .zip file, and give the command

unzip -l name-of-the-zip-file.zip

to see what is inside the file.

Give the command

unzip -l name-of-the-zip-file.zip

to unpack the contents into your current working directory, or give the command

unzip -l name-of-the-zip-file.zip -d path-to-another-directory

to unpack the contents into some other directory.

8.6 On the exam, why didn’t I get partial credit for the multiple-answer questions?

On multiple-answer (choose all that apply) questions:

  1. It is equally important to choose the correct items and to not choose the incorrect items. (Otherwise you could always get a perfect core by selecting all of the items.)
  2. Partial credit is calculated by dividing the total points possible by the number of correct answers for the question. That’s how much you gain for each correct answer selected and how much you lose for each incorrect answer selected.
    • For example, if a question has 5 options, two of which are correct, each of those two correct answers earns 50% of the points, but each incorrect answer selected subtracts 50%.
    • If a question has 3 options, only one of which is correct, then that option is worth 100% of the points, and selecting either of the incorrect options will subtract 100%.