1

It's been a while since I've written in C++, so I've been looking through some old code just to get the beginning of my program started.

I have the following in a file called "branchsim.hpp":

#ifndef _BRANCHSIM_HPP
#define _BRANCHSIM_HPP

// some #include statements here

using std::cin;
using std::cout;
// ... some more using statements here

class BranchSim {
public:
    int readMode(&string mode);
};


#endif

and I have this in a file called branchsim.cpp:

#include "./branchsim.hpp"

int main(void) {
    string mode;

    return EXIT_SUCCESS;
}

/*
 *  Returns an int representing the branch prediction heuristic to be 
used.
 */
int BranchSim::readMode(&string mode) {
    switch (tolower(mode)) {
        case "at":          // always taken
            return 1;
        case "nt":          // never taken
            return 2;
        case "btfn":        // bkwd taken, fwd not taken
            return 3;
        case "bimodal":     // bimodal
            return 4;
        case "twolevel":    // dynamic two level
            return 5;
        default:
            cout << "Invalid prediction mode entered.\n";
    }

    return 0;
}

and I'm getting the following errors:

./branchsim.cpp:35:24: error: ‘int BranchSim::readMode’ is not a 
static data member of ‘class BranchSim’
 int BranchSim::readMode(&string mode) {
                    ^
./branchsim.cpp:35:33: error: expected primary-expression before 
‘mode’
 int BranchSim::readMode(&string mode) {
                             ^
./branchsim.cpp:35:39: error: expected ‘,’ or ‘;’ before ‘{’ token
 int BranchSim::readMode(&string mode) {
                                   ^
Makefile:16: recipe for target 'branchsim.o' failed
make: *** [branchsim.o] Error 1

I'm just trying to get this to compile so I can continue writing the rest of the program, but am getting stuck.

I believe my syntax is correct..I'm just a bit unsure of how to fix these errors. As I said, my C++ is a bit rusty so this could very well be something quite obvious that I'm missing/forgetting but any help would be greatly appreciated!

EDIT: I'd simply misplaced the & (belongs after string, not before) and I quickly realized c++ doesn't allow switch on strings, so I've adjusted accordingly.

Apologies for the trivial question but thank you for the quick help!

2
  • 1
    Firstly, what were you trying to say by that &string syntax? Secondly, I find it hard to believe that your first error message is as quoted. You should have gotten an earlier error for &string in your header file. Commented Nov 27, 2017 at 20:13
  • That was the error -- simply meant to put the '&' after 'string'. Also had to modify the switch as I forgot C++ doesn't allow this on strings, but all is well now. Commented Nov 27, 2017 at 20:52

1 Answer 1

2

You probably mean string& mode as in "reference to a string" not &string mode as in "address of string". Typical C++ code would have this signature look like:

int BranchSim::readMode(const string& mode);

Where the implementation follows the same spec.

2
  • 1
    Ah that was pretty stupid of me. Thank you -- have some other errors that I'll fix but was worried I was doing something fundamentally wrong. Appreciate the help! Commented Nov 27, 2017 at 20:19
  • No trouble. &string is valid syntax, but it's just not supposed to be in a signature like that, so that's why you're getting an unfamiliar error. If you're committed to learning C++ the book by Bjarne, the creator of C++ is pretty much required reading as it gets into all of this and more.
    – tadman
    Commented Nov 27, 2017 at 21:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.