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.
Related
I need to instantiate an object from a class, let's say Cat, and know the names of its included files.
The main program should look something like this:
#include <iostream>
#include <vector>
#include "Cat.h"
using namespace std;
vector<string> getIncludes(Cat cat){
...
}
int main(int argc, char *argv[])
{
Cat cat;
std::vector<string>list = getIncludes(cat);
}
And the Cat class header file would be like this:
#include "utils.h"
#include "animals.h"
class Cat{
public:
Cat();
~Cat();
void meow();
private:
int age;
}
After calling std::vector<string> list = getIncludes(cat), list should contain "utils.h" and "animals.h".
If there is another way to get those names in the main function I am open to suggestions. Thank you in advance.
No, that's not possible, the information won't be present anymore at runtime.
The preprocessor simply replaces the contents of utils.h and animals.h at compile time at the place where the #include statements appear.
If you need such a list to detect dependencies for recompiling your code, most of the compilers can generate a list of the included headers in a translation unit.
Here's some further information how to do so:
GCC toolchain
MSVC toolchain
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.
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.
After fixing the previous problem (see my one other question that I have asked). I had declared more classes.
One of these is called CombatAdmin which does various things: (Header file)
#ifndef COMBATADMIN_H
#define COMBATADMIN_H
#include <string> // Need this line or it complains
#include <Player.h>
#include <Sound.h>
#include <Enemy.h>
#include <Narrator.h>
using namespace std;
class Enemy;
class Player;
class CombatAdmin // Code yet to be commented here, will come soon.
{
public:
CombatAdmin();
void healthSet(double newHealth, string playerName);
void comAdSay(string sayWhat);
void playerFindsChest(Player *player,Weapon *weapon,Armour *armour);
void youStoleOurStuffEncounter(Player *player);
void comAdWarning(string enemyName);
void comAdAtkNote(string attack, double damage,string target,string aggresor);
void entDefeated(string entName);
void comAdStateEntHp(string ent, double hp);
void comAdStateScanResults(string enemyName, double enemyHealth);
string doubleToString(double number);
string intToString(int number);
bool isRandEncounter();
void randomEncounter(Player *player,Sound *sound,Narrator *narrator);
bool combatRound(Player *player, Enemy *enemy, Sound *sound, bool ran);
void playerFindsItem(string playerName,string itemName,double itemWeight,double playerWeight);
void playerFindsGold(string playerName,double coinCnt,double playerCoinCnt);
};
#endif // COMBATADMIN_H
It is then instanced in the main.cpp file like this: (Snippet of the main.cpp file)
#include <iostream> // Required for input and output
#include <Item.h> // Item header file.
#include <Weapon.h> // Header files that I have made for my classes are needed for this program
#include <sstream> // Needed for proper type conversion functions
#include <windows.h> // for PlaySound() and other functions like sleep.
#include <time.h> // Needed to seed the rand() function.
#include <mmsystem.h> // Not sure about this one, possibly defunct in this program.
#include <stdio.h> // Needed for a similar kind of output as iostream for various functions error msgs.
#include <irrKlang.h> // The header file of the sound lib I am using in this program.
#include <Narrator.h> // The narrators's header file.
#include <Pibot.h> // Other header files of classes.
#include <Armour.h>
#include <Player.h>
#include <Weapon.h>
#include <CombatAdmin.h>
using namespace irrklang;
using namespace std;
// Forward referenced functions
void seedRandom(); // Seeds the random number so it will be random as apposed to pseudo random.
string getPlayerName(string temp); // Gets the player's new name.
int main(int argc, char* argv[])
{
// Variables and object pointers declared here.
CombatAdmin *comAd = new CombatAdmin(); // Handles combat.
Narrator *narrator = new Narrator(); // The Narrator that says stuff
Pibot *piebot = new Pibot(); // PIbot, the player's trusty companion
string temp; // Temp string for input and output
However, when I try to compile the project, I get the following error:
C:\Documents and Settings\James Moran.HOME-B288D626D8\My Documents\C++ projects\Test Project\main.cpp|59|undefined reference to `CombatAdmin::CombatAdmin()'|
I am using the Code::Blocks IDE (ver 10.05), with the GNU GCC compiler. The project is of type "Console application". I am using windows XP 32 bit SP3.
I have tried changing to search directories to include where the object files are, but no success there.
As can be seen from the code, the narrator and PIbot are instanced just fine. (then used, not shown)
My question is, therefore, what do I need to do to stop these errors occurring? As when I encountered similar "Undefined reference to x" errors before using libraries. I had just forgotten to link to them in Code::Blocks and as soon as I did, they would work.
As this class is of my own making I am not quite sure about this.
Do say if you need more information regarding the code etc.
You have declared the default constructor (CombatAdmin()) and thus prevented the compiler from automatically generating it. Thus, you either need to 1) remove declaration of the default constructor from the class, or 2) provide an implementation.
I had this kind of error and the cause was that the CombatAdmin.cpp file wasn't selected as a Build target file: Prject->Properties->Build targets
Are you sure you've to include your header as:
#include <CombatAdmin.h>
?
I think you need to include your header file as:
#include "CombatAdmin.h"
And same for other headers written by you, like these:
#include "Armour.h"
#include "Player.h"
#include "Weapon.h"
//and similarly other header files written by you!
See this topic:
What is the difference between #include <filename> and #include "filename"?
My solution was just to add a line in the header before the class defenition:
class CombatAdmin;
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.