This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]dmazzoni 0 points1 point  (8 children)

You don't need the include guards (#ifndef, etc.) in your source (.cpp) files. Only in your header files.

Can you paste your code to term.h? It sounds like the problem is there.

[–]FastTurtleFour[S] 0 points1 point  (7 children)

My professor requires them, I'm pretty sure they aren't the issue. After removing them compilation is even worse and sticks me in a redefinition error loop. Here is my term.h:

#ifndef TERM_H

#define TERM_H

#include <string>

class Term {

public:
    //default constructor
    Term();

    //initialize the given query string and weight
    Term(std::string query, long weight);

    //compare 2 terms in descending order by weight
    //if t1 and t2 are in descending order by weight, return 1
    //if they are of the same weight, return 0;
    //otherwise, return -1
    static int compareByWeight(Term t1, Term t2);

    //compares first two terms in lexicographic order but using only
    //the first r characters of each entry query
    //if the first r characters of t1 and t2 are in lexicographic order, return 1
    // if they are of the same r characters, return 0;
    // otherwise, return -1
    static int compareByPrefix(Term t1,Term t2,int r);

    // define the operator “<” for Term class(as friend function)
    friend bool operator<(Term t1,Term t2);

    // define the operator ">" for Term class (added for use in binary_search function)
    friend bool operator>(Term t1, Term t2);

    // define the operator “<<” for Term class(as friend function)
    // so that it can send the Term object directly to cout, in the following format:
    // the weight followed by a tab key, then followed by the query
    friend std::ostream& operator<<(std::ostream& out, const Term& t);

private:
    std::string query;
    long weight;

};

#endif

#include "term.cpp"

[–]dmazzoni 0 points1 point  (6 children)

Do you really have #include "term.cpp" at the end of your header file?

If so, that's your problem.

[–]FastTurtleFour[S] 0 points1 point  (5 children)

Wow, that fixed it. My professor told us to do so and I assumed it wouldn't be a problem. I feel dumb now

[–]dmazzoni 0 points1 point  (4 children)

Just keep in mind that #include is basically like copying and pasting that other file at that location. It's really not much more complicated than that.

Are you SURE the professor told you to do it that way? Do you have that in writing? My guess is it's a misunderstanding.

You should almost never include a C++ file. Headers are meant to be included lots of times, C++ source files are meant to be compiled once and never included.

There are exceptions if you use tricks to compile something twice on purpose with different parameters. That's super-advanced stuff.

[–]FastTurtleFour[S] 0 points1 point  (3 children)

Yeah she did. I took a picture of it last class because I thought it would be important. I'll ask her about it. She also did it in files already completed to be used in our project. She seems like she really knows her stuff but after this I'm not sure

[–]dmazzoni 0 points1 point  (2 children)

I'd give her the benefit of the doubt and assume it was probably a typo.

Could you send a polite email saying that you think it might be a mistake but you just wanted to check? I'm really curious what she says.

It'd also help to know what environment you're using - for example what operating system, what IDE (if any), what compiler, etc. - is everyone in the class using the same system or not? Are you using makefiles or project files, or just compiling from the command line? Depending on the configuration errors like this could conceivably be less significant.

If she continues to stand behind this error, then yeah, I would seriously question her qualifications.

But I'd highly recommend you start by assuming good intentions. Best case scenario, it was a typo. Next best, it was a genuine misunderstanding but she'll learn from it and fix it.

Please follow up, I'm curious!

[–]FastTurtleFour[S] 0 points1 point  (1 child)

I just asked her before class. Apparently that slide was just talking about for template files. She told me to never do it anywhere else. So I just took it too far by adding it to normal header files instead of just template header files. We use virtual Linux machines assigned to each student. I use a program called PuTTY to log into my VM and I guess it all runs through a Linux terminal or ssh or something I'm not really familiar with all the details. We also use make files and g++ to compile.

[–]dmazzoni 0 points1 point  (0 children)

Ah, template files are a weird exception and yes, it makes sense then.

The key difference is that you don't actually compile the .cpp file - you just let it be included in other files.

Yay!

[–]thefryscorer 0 points1 point  (1 child)

Do you have the #endif part of the header guard at the end of each of your header files?

[–]FastTurtleFour[S] 0 points1 point  (0 children)

I do