Having problems with an extra qualification error in c++11 compiler - c++

I'm a c++11 student and I'm having trouble with an extra qualification error.
I have a class declared in a .h file and the implementation for a boolean function in a separate .cpp file.
The class definition is as follows:
class Order{
std::string customer, product;
std::vector<std::string> itemList;
bool validName(std::string name);
bool isCustomerName(std::string name);
bool isProductName(std::string name);
bool isItemName(std::string name);
public:
Order(std::vector<std::string> line);
void print(){
void graph(std::ofstream os);
};//class Order
all of the functions are implemented in a separate cpp file, and I have scoped all of the functions in the following manner:
Order::Order(std::vector<std::string> line){
or
bool Order::isCustomerName(std::string name){
When I try to compile the cpp file, this error comes up:
error: extra qualification ‘Order::’ on member ‘Order’ [-fpermissive]
After looking it up, it seems to be an error related to using the scope operator either in the class definition on the same function or some kind of double use of the scope operator.
I haven't encapsulated the implementations in the cpp file in a separate namespace and I have only included the corresponding .h file for the cpp file. Can someone please give me a little push in the direction I need to look at to solve this issue?
Thanks
This is the top of the cpp file:
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include "order.h"
this is a sample function from the same cpp:
bool Order::isProductName(std::string name){
if (name.size() > 0 && isalpha(name[0]))
return true;
return false; }
The class definition listed above is literally everything that's in the .h for class Order.
the top of the .h is:
#pragma once
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include "util.h"

You have this line in your class:
void print(){
I believe you meant
void print();
Because of the way C++ compiles, when you say #include "order.h" the compiler is literally copy and pasting the contents of order.h into your cpp file. So it sees that you have opened this function definition for print, and declared some local functions inside of your member function print (a gcc extension), and then you eventually close the function out at the line labeled };//class Order. This looks to you like the end of the class definition, but it's actually the end of your function. The function definitions later on that are in your cpp file are seen as being inside the class body, which confuses the compiler.

Related

Object listed in header file, yet undefined reference in cpp file

In the class, in my header file, in the private section of the class I have
Heap_PriorityQueue<Passenger> oldBoardingQueue;
I have #include "Heap_PriorityQueue.h" in both the header and the cpp just to be safe.
I haven't even started using it in my cpp file, yet when I try to compile the cpp file throws up a bunch of:
undefiend reference to 'Heap_PriorityQueue<Passenger>::(insert function for Heap_PriorityQueue class isEmpty, add, etc.)
Followed by several
undefined reference to 'non-virtual thunk to Heap_PriorityQueue<Passenger>::(Heap_PriorityQueue functions again)
Unsure of how to proceed. Am I declaring incorrectly?
Edit:
Passenger is another class for creating Passenger objects holding the passenger's data (name, row, priority/key, etc).
Not sure what's meant by 'linker command' (I'm a student/newb) but the makefile for airworthy.cpp and airworthy.h files are as follows:
Airworthy.o: Airworthy.cpp Airworthy.h Heap_PriorityQueue.h NotFoundException.h PrecondViolatedExcep.h Passenger.h
g++ -std=gnu++11 -ggdb -c Airworthy.cpp
Airworthy.h is as follows:
#ifndef AIRWORTHY_H
#define AIRWORTHY_H
#include "Heap_PriorityQueue.h"
#include "Passenger.h"
using namespace std;
class Airworthy
{
private:
int oldBoardingSeconds;
int randomBoardingSeconds;
Heap_PriorityQueue<Passenger> oldBoardingQueue;
Heap_PriorityQueue<Passenger> randomBoardingQueue;
Passenger thisPassenger;
public:
Airworthy();
void inputFileData(ifstream& inputFile);
void loadQueue(ifstream& inputFile, ofstream& outputFile);
void runSim();
void setOldBoardingPriority();
void setRandomBoardingPriority();
};
#endif
Just realized that the error hones in on the first use of the Airworthy constructor which I've just had empty for now. Here's the first part of Airworthy.cpp
#include "Airworthy.h"
#include "Heap_PriorityQueue.h"
#include "Passenger.h"
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;
Airworthy::Airworthy()
{
}
Tried putting different things inside the constructor (including the Heap_PriorityQueues) but doesn't seem to make a difference. Calling the constructor in the main cpp file makes that file also generate the same errors.
Heap_PriorityQueue.h is as follows:
#ifndef _HEAP_PRIORITY_QUEUE
#define _HEAP_PRIORITY_QUEUE
#include "ArrayMaxHeap.h"
#include "PriorityQueueInterface.h"
template<class ItemType>
class Heap_PriorityQueue : public PriorityQueueInterface<ItemType>,
private ArrayMaxHeap<ItemType>
{
public:
Heap_PriorityQueue();
bool isEmpty() const;
bool add(const ItemType& newEntry);
bool remove();
/** #pre The priority queue is not empty. */
ItemType peek() const throw(PrecondViolatedExcep);
}; // end Heap_PriorityQueue
#endif
I'm at my wits end, I've been trying to make this damned thing work for hours now.
I think the likely problem is that you forgot to include Heap_PriorityQueue.cpp in the makefile recipe for Airworthy.o. So the compiler is finding the definitions for the functions of the queue, but not the funtions themselves (that is the meaning of "undefined reference" in the linking stage)
EDIT: check if the build process produces a Heap_PriorityQueue.o file, and if this file is included in the final commandline that creates the exe.

Expected ')' before string, STD:: not fixing

In an .h file, I have the following code.
#ifndef COUNTEDLOCATIONS
#define COUNTEDLOCATIONS
#include <iostream>
#include <string>
struct CountedLocations {
CountedLocations();
CountedLocations(std::string url, int counter);
std::string url;
int count;
//below is code for a later portion of the project
bool operator== (const CountedLocations&) const;
bool operator< (const CountedLocations&) const;
};
In my .cpp file that includes the .h file, my code is
#include "countedLocs.h"
#include <iostream>
#include <string>
using namespace std;
CountedLocations(std::string url, int counter)
{
}
I get the error "Expected ')' before 'url'. I've tried commenting out the empty constructor in the .h file, I've tried messing with semicolons, I've tried removing the std:: that prefixes the 'string url', but nothing seems to work. I tried looking at a similar problem on StackOverflow, but all three of the solutions do nothing. How can I fix this?
EDIT: Originally, I had
CountedLocations::CountedLocations(std::string url, int counter)
instead of
CountedLocations(std::string url, int counter)
But that gave me the error "Extra qualification 'CountedLocations::' on member 'CountedLocations' [-fpermissive], so I elected not to use it.
Move need the #include <string> from the .cpp to the .h file so that the file countedLocs.h knows about std::string definition. In your case with one cpp you can switch the order of includes but it would be better to have it in the header there (countedLocs.h) if you plan to use it in other places also.
#include <iostream>
#include <string>
#include "countedLocs.h"
If this is really all of your code then you don't have a definition of std::string (ie #include ) before your struct is defined.
.h files should be able to be compiled all by themselves. put #include in the .h file (and some include guards too!)
In your cpp file (not your header file), you should have this:
CountedLocations::CountedLocations(std::string url, int counter)
{
}
not this:
CountedLocations(std::string url, int counter)
{
}
But that gave me the error "Extra qualification 'CountedLocations::'
on member 'CountedLocations' [-fpermissive], so I elected not to use
it.
That's the error you would get if you put the qualification on the declaration of the constructor in your class body.

C++ error no such file or directory

Hey guys, I just started learning C++ and I have this code that I have to complete but the class header is giving me this error
error: string: No such file or directory
#include <vector>
#include <string>
class Inventory
{
public:
Inventory ();
void Update (string item, int amount);
void ListByName ();
void ListByQuantity ();
private:
vector<string> items;
};
your code should probably look something more like this:
#include <string>
#include <vector>
class Inventory {
public:
Inventory();
void Update(const std::string& item, int amount);
void ListByName();
void ListByQuantity();
private:
std::vector<std::string> items;
};
if #include <string> is in fact your include directive, then you may be compiling the file as a c program. the compiler often determines language by the file extension. what is your file named?
I don't think your error is anything to do with namespaces.
You say you're getting error: string: No such file or directory which implies that the pre-compiler cannot find the STL string definition file. This is quite unlikely if you're also including vector and having no problems with that.
You should check your compilation output for clues about where it's picking header files from. Any chance you could post the full compilation output?
Either use using std::string (not recommended) or replace string with std::string.
Or, if I have misunderstood, use #include <string> instead of #include "string".
Same goes for vector which is also in std namespace.

error C2039: 'string' : is not a member of 'std', header file problem

I am having problems with a class I am writing. I have split the class into a .h file that defines the class and an .cpp file that implements the class.
I receive this error in Visual Studio 2010 Express:
error C2039: 'string' : is not a member of 'std'
This is the header FMAT.h
class string;
class FMAT {
public:
FMAT();
~FMAT();
int session();
private:
int manualSession();
int autoSession();
int mode;
std::string instructionFile;
};
This is the implementation file FMAT.cpp
#include <iostream>
#include <string>
#include "FMAT.h"
FMAT::FMAT(){
std::cout << "manually (1) or instruction file (2)\n\n";
std::cin >> mode;
if(mode == 2){
std::cout << "Enter full path name of instruction file\n\n";
std::cin >> instructionFile;
}
}
int FMAT::session(){
if(mode==1){
manualSession();
}else if(mode == 2){
autoSession();
}
return 1;
}
int FMAT::manualSession(){
//more code
return 0;
}
this is the main file that uses this class
#include "FMAT.h"
int main(void)
{
FMAT fmat; //create instance of FMAT class
fmat.session(); //this will branch to auto session or manual session
}
My inability to fix this error is probably a result of me not understanding how to properly structure a class into separate files. Feel free to provide some tips on how to handle multiple files in a c++ program.
You need to have
#include <string>
in the header file too.The forward declaration on it's own doesn't do enough.
Also strongly consider header guards for your header files to avoid possible future problems as your project grows. So at the top do something like:
#ifndef THE_FILE_NAME_H
#define THE_FILE_NAME_H
/* header goes in here */
#endif
This will prevent the header file from being #included multiple times, if you don't have such a guard then you can have issues with multiple declarations.
Your FMAT.h requires a definition of std::string in order to complete the definition of class FMAT. In FMAT.cpp, you've done this by #include <string> before #include "FMAT.h". You haven't done that in your main file.
Your attempt to forward declare string was incorrect on two levels. First you need a fully qualified name, std::string. Second this works only for pointers and references, not for variables of the declared type; a forward declaration doesn't give the compiler enough information about what to embed in the class you're defining.
Take care not to include
#include <string.h>
but only
#include <string>
It took me 1 hour to find this in my code.
Hope this can help

C++ include statement required if defining a map in a headerfile

I was doing a project for computer course on programming concepts. This project was to be completed in C++ using Object Oriented designs we learned throughout the course. Anyhow, I have two files symboltable.h and symboltable.cpp. I want to use a map as the data structure so I define it in the private section of the header file. I #include <map> in the cpp file before I #include "symboltable.h".
I get several errors from the compiler (MS VS 2008 Pro) when I go to debug/run the program the first of which is:
Error 1 error C2146: syntax error : missing ';' before identifier 'table' c:\users\jsmith\documents\visual studio 2008\projects\project2\project2\symboltable.h 22 Project2
To fix this I had to #include <map> in the header file, which to me seems strange.
Here are the relevant code files:
// symboltable.h
#include <map>
class SymbolTable {
public:
SymbolTable() {}
void insert(string variable, double value);
double lookUp(string variable);
void init(); // Added as part of the spec given in the conference area.
private:
map<string, double> table; // Our container for variables and their values.
};
and
// symboltable.cpp
#include <map>
#include <string>
#include <iostream>
using namespace std;
#include "symboltable.h"
void SymbolTable::insert(string variable, double value) {
table[variable] = value; // Creates a new map entry, if variable name already exist it overwrites last value.
}
double SymbolTable::lookUp(string variable) {
if(table.find(variable) == table.end()) // Search for the variable, find() returns a position, if thats the end then we didnt find it.
throw exception("Error: Uninitialized variable");
else
return table[variable];
}
void SymbolTable::init() {
table.clear(); // Clears the map, removes all elements.
}
My guess is that you have another file that includes the header file #include "symboltable.h". And that other source file doesn't #include <map> nor #include <string> nor has using namespace std before it includes "symboltable.h".
Check which file is being compiled when you get the error. Is it maybe a different source file than the .cpp that you mentioned? Possibly something like main.cpp?
Another way to solve your problem is to put the includes you need in your header file and use std::map instead of simply map. Also you use string which is also inside the namespace std. So that needs to be std::string. And put the missing #include <string>.
Yes, you indeed have to #include <map> in the header file.
You use map in the declaration of the class, so the compiler needs to know what this map refers to. Since the definition of it is in <map> you need to include that header before using the map template class.
You could also instead #include <map> in every source file before the #include "symboltable.h" line, but usually you would just include these kind of prerequisites in the header.