using vector in c++ class definition - c++

I have a mysterious problem. I keep getting a ‘vector’ does not name a type error when trying to define a variable in the tour class. The library seems installed correctly, since I can define vectors in main. According to many posts here I have checked for the right vector includes and std namespace. Still I get the error.
main.cpp
#include <vector>
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <cmath>
#include <cstdlib>
#include "tour.h"
using namespace std;
int main () {
//vector declaration works here
return 0;
}
tour.h
#ifndef TOUR_H_
#define TOUR_H_
#include<vector>
using namespace std;
class tour
{
public:
//variables
int teamID;
vector<int> tourseq; //this is where the error occurs
//functions
tour(int);
};
#endif /* TOUR_H_ */
tour.cpp
#include<vector>
#include "tour.h"
using namespace std;
tour::tour(int id){
teamID = id;
}
What could be wrong here?

Instead of writing using namespace std; and vector<int> tourseq; consider writing std::vector<int> tourseq;.
You probably shouldn't put using namespace std; in your code anyway.

Related

C++ 'vector' was not declared in this scope

I'm getting an error everytime I compile the function.cpp file saying that stocks and newStock are not declared in this scope. I'm trying to use a struct inside a vector. Thanks for the help.
This is the main.cpp file
#include <fstream>
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include <vector>
using namespace std;
struct Stocks
{
int one;
int two;
int three;
};
vector<Stocks> portfolio;
#include "testProject2.h"
int main()
{
buyStock(portfolio);
}
This is the header file.
#include <iostream>
void buyStock(vector<Stocks>& Portfolios);
This is the function.cpp file
#include <iostream>
#include <vector>
#include "testProject2.h"
void buyStock(vector<Stocks>& Portfolios)
{
Stocks newStock;
newStock{1,2,3};
Portfolios.push_back(newStock);
}
Your function.cpp file has no way to know what the Stocks struct is. Define it in the header file:
struct Stocks {
int one;
int two;
int three;
};
And remove its definition from main.cpp.
Also in your header file, you need
#include <vector>
and refer to vector parameter as std::vector<Stocks> &Portfolios (better than using namespace std;)
Your initialization syntax newstock{1,2,3} looks incorrect too.
You use vector in your header file without it being defined.
Try changing the header file to this:
#include <vector>
#include <Stocks.h> // name of .h file where Stocks is defined
void buyStock(std::vector<Stocks>& Portfolios);
// OR
using namespace std::vector;
void buyStock(vector<Stocks>& Portfolios);

Reference to "class" is ambigous

I would like to implement a hash table example.
So for this aim, I have created one header, one hash.cpp and main.cpp files.
in my hash.cpp , I tried to run a dummy hash function which takes key value and turns into an index value. however, it throws an error(reference to 'hash' is ambiguous) whenever I try to create an object according to that hash class.
this is my main.cpp:
#include "hash.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdio.h>
using namespace std;
int main(int argc, const char * argv[]) {
hash hash_object;
int index;
index=hash_object.hash("patrickkluivert");
cout<<"index="<<index<<endl;
return 0;
}
this is my hash.cpp:
#include "hash.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdio.h>
using namespace std;
int hash(string key){
int hash=0;
int index;
index=key.length();
return index;
}
this is my hash.h
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
#ifndef __hashtable__hash__
#define __hashtable__hash__
class hash
{
public:
int Hash(string key);
};
#endif /* defined(__hashtable__hash__) */
Your hash class symbol is clashing with std::hash
A quick fix could be using a global namespace qualifier
int main(int argc, const char * argv[]) {
::hash hash_object;
but a better and recommended one would be to stop polluting your global namespace with
using namespace std;
and just using std::cout or std::endl when you need them.
You could also create your own namespace in case you're writing a library.
Besides, you have some capital letter typos here:
index = hash_object.hash("patrickkluivert");
^ I suppose you're referring to the Hash() function here
and here
int Hash(std::string key) {
^ this needs to be capital as well
int hash = 0;
in case you want to match your declaration and avoid cast/linking errors.
Your hash class is conflicting with std::hash. Stop using using namespace std; right now. If you want to make print statements shorter, try using std::cout; using std::endl;

Undefined reference to header file function

I got an error of C:\temp\hashTableProject\main.cpp|14|undefined reference to hash::Hash(std::string)
Anyone know how to solve this problem?
hash.h
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
#ifndef HASH_H
#define HASH_H
class hash{
public:
int Hash(string key);
};
#endif // HASH_H
hash.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include "hash.h"
using namespace std;
int hash::Hash(string key){
//int hash = 0;
int index;
index = key.length();
return index;
}
main.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include "hash.h"
using namespace std;
int main()
{
int index;
hash hashOb;
string traget = "Testing";
index = hashOb.Hash(traget);
cout << index << endl;
return 0;
}
Im using CodeBlock 13.12
There is only main.o file in obj folder. I dont know why the hash.o isn't there.
hash is an inbuilt template in "std" namespace.
Try removing using namespace std; lien and use namespace name as and when required.

Trouble with using class types within other classes

I have absolutely no idea what's going on. I've been looking up explanations for the weirdness going on here but it seems my situation is in some ways unique. I imagined it was the order in which I include my header files in each of my files, but to no avail, I have not found a combination that seems to be the solution.
The exact error I seem to be getting is "log does not name a type" when declaring LogArray[maxLength].
One of my classes, class logmgmt:
class logmgmt
{
private:
static const int maxLength = 500;
log LogArray[maxLength];
int length;
public:
void fillLogs(int index, int iD, std::string date, double startTime, double endTime);
void displayThisLog(int index);
void setLength(int length);
};
Pre-processor directives within logmgmt.cpp:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
#include "log.h"
#include "Logmgmt.h"
And directives within main.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
#include "employee.h"
#include "log.h"
#include "employeemgmt.h"
#include "Logmgmt.h"
Remove using namespace std.
That is polluting the global namespace with lots of symbol names that can cause these conflicts.
In your example, the function std::log becomes log. So it can no longer name a global type.

"Unresolved External Symbol" errors when creating object in C++

I'm a pretty seasoned programmer but I'm just now diving into C++ and it's... well... more difficult than PHP and Python. I keep having unresolved external errors when trying to create an object from some classes. It's broken up into multiple headers and files but here is a basic idea from one of my classes:
die.h:
#ifndef DIE_H
#define DIE_H
using namespace std;
class Die {
public:
int throwDie();
Die();
};
#endif
die.cpp
#include <iostream>
#include <cstdlib>
#include "Die.h"
using namespace std;
int Die::throwDie()
{
return 0;
}
sixsidedie.h
#ifndef SIXSIDEDIE_H
#define SIXSIDEDIE_H
#include "Die.h"
using namespace std;
class SixSideDie : public Die
{
public:
SixSideDie();
int throwDie();
private:
int randNumber;
};
#endif
sixsidedie.cpp
#include <iostream>
#include <cstdlib>
#include <time.h>
#include "Die.h"
#include "SixSideDie.h"
using namespace std;
const int SIX_SIDE = 6;
int SixSideDie::throwDie()
{
srand((unsigned int)time(0));
SixSideDie::randNumber = rand() % SIX_SIDE + 1;
return SixSideDie::randNumber;
}
main.cpp
#include <iostream>
#include <cstdlib>
#include "Die.h"
#include "SixSideDie.h"
#include "TenSideDie.h"
#include "TwentySideDie.h"
using namespace std;
int main()
{
Die* myDice[3];
myDice[0] = new SixSideDie();
myDice[1] = new TenSideDie();
myDice[2] = new TwentySideDie();
myDice[0]->throwDie();
myDice[1]->throwDie();
myDice[2]->throwDie();
system("pause");
return 0;
}
It keeps telling me that each object I create directly above is an unresolved external symbol and I just don't know why. Any thoughts!?
You declared a constructor for Die but never defined it.
Also, you almost certainly want throwDie to be virtual if you intend to override its behavior in derived classes, and you should never use using namespace std; in a header file (and many people, including me, would argue that you shouldn't use it at file-scope at all).
You didn't define your constructor in your cpp files.
Its good practice to define the constructors of the classes. Check this out:
#ifndef DIE_H
#define DIE_H
using namespace std;
class Die {
public:
int throwDie();
Die() { }; // can you spot the difference here?
};
#endif