I apologize for asking a question that should have a simple solution, but it's driving me nuts. I've checked for all the common errors: namespace std, spelling, include vector, etc. Below is the abbreviated code for my video.h file.
#include <iostream>
#include <string>
#include <new>
#include <vector>
using namespace std;
class Video
{
public:
Video(string, string, string, float, int);
vector<Video*> video_ptrs;
void print();
};
And here is the code for my main.cpp
#include "video.h"
using namespace std;
int main()
{
...
Video* temp_here = new Video(title, url, comment, length, rating);
video_ptrs.push_back(temp_here);
return 0;
}
The error that returns says, "'video_ptrs' was not declared in this scope." Thank you ahead of time for any help given.
video_ptrs is a member of Video, call it with the object you just created:
Video* temp_here = new Video(title, url, comment, length, rating);
temp_here->video_ptrs.push_back(temp_here);
This adds a pointer temp_here to the vector of that same object though, I'm not sure if this is intended.
video_ptrs is indeed not declared in main. You shall use
temp_here->video_ptrs...
Related
I've just now come across an error using arrays that seems odd, I've searched the web but it would appear this is typically something people run into when they are dealing with multidimensional arrays. The error occurs when I attempt to call a function that uses an array as a parameter. Here is the code:
#include <iostream>
#include <string>
#include <iomanip>
#include "header.h"
#include <fstream>
using namespace std;
int main(){
string make, model, licensePlate, address, name, phoneNumber;
int year, messageCode;
char choice;
bool ready[10];
string phoneNumber[10], name[10], address[10], licensePlate[10], make[10], model[10];
int year[10];
initializeArray(year[]);
The error occurs within the brackets of year[], expected an expression. Thanks in advance!
I apologize for my poor code!
Having both variables of int and an array with the same name was a terrible idea, as you do not use the brackets to pass an array. I removed the brackets and the int year variable.
Here is the revised work:
bool ready[10];
string phoneNumber[10], name[10], address[10], licensePlate[10], make[10], model[10];
int year[10];
initializeArray(year);
I have the following code in a project:
#ifndef MAP_H
#define MAP_H
#include <string>
#include "MapCell.h"
using namespace std;
class Map{
public:
Map();
Map(int, int);
Map(string);
virtual ~Map();
private:
int grid_size;
MapCell * grid;
};
#endif
When I go to compile, I get the error "error: 'MapCell' does not name a type" yet when I comment out MapCell * grid and run this next block of code from main:
#include <iostream>
#include <cstdlib>
#include "MapCell.h"
using namespace std;
int main() {
MapCell * test_var;
test_var = new MapCell();
delete test_var;
cout << "Press enter to end process...";
cin.get();
cin.get();
return 0;
}
everything works just fine. I know my MapCell.h and .cpp files are in the right places and I'm guessing the compiler can see them since it works from main. I read around some other questions and most answers seem to point to either syntax errors or forward declarations which don't really fit here (unless I'm overlooking something)
Any ideas on what's going on here?
chris and greatwolf led me to the solution that fixed the problem. I needed to forward declare my MapCell class for the compiler to be able to link to the whole class.
I have a class which is going to handle an array of objects of another class I've created earlier (which works fine). The problem appears when I try to create an object of my List-class.
This is the header of the list-class:
#ifndef personlistH
#define personlistH
#include "Person.h"
#include <iomanip>
#include <iostream>
#define SIZE 10
namespace std {
class PersonList {
private:
Person persons[SIZE];
int arrnum;
string filename;
public:
Personlist();
};
}
#endif
This is the main function:
#include <iostream>
#include "PersonList.h"
using namespace std;
int main() {
PersonList personlist;
return 0;
}
The error my compiler is giving me is the following:
error: "27 \PersonList.h ISO C++ forbids declaration of `Personlist'
with no type"
I've searched for answers but as I'm quite new to C++ it's been a bit confusing and I haven't found any fitting yet. It would be great if you could explain this error for me.
You have the wrong capitalisation on your constructor declaration. You have Personlist(); but need PersonList();. Because what you have isn't equal to the class name it is considered a function rather than a constructor, and a function needs a return type.
Do not add your own types to the standard namespace(std), instead create your own namespace and define your class inside it.
//PersonList.h
namespace PersonNamespace
{
class PersonList
{
//members here
};
}
//Main.cpp
using namespace PersonNamespace;
The actual error is that you made a typo in Personlist instead of PersonList
The error is because you got the capitalisation wrong when you declared the constructor; it should be PersonList() not Personlist().
Also, you should never declare your own classes in the std namespace; that's reserved for the standard library. You shoud make up your own namespace name, and put your things in that.
I am receiving the error: identifier "string" undefined.
However, I am including string.h and in my main file, everything is working fine.
CODE:
#pragma once
#include <iostream>
#include <time.h>
#include <string.h>
class difficulty
{
private:
int lives;
string level;
public:
difficulty(void);
~difficulty(void);
void setLives(int newLives);
int getLives();
void setLevel(string newLevel);
string getLevel();
};
Can someone please explain to me why this is occurring?
<string.h> is the old C header. C++ provides <string>, and then it should be referred to as std::string.
You want to do #include <string> instead of string.h and then the type string lives in the std namespace, so you will need to use std::string to refer to it.
You forgot the namespace you're referring to. Add
using namespace std;
to avoid std::string all the time.
Because string is defined in the namespace std. Replace string with std::string, or add
using std::string;
below your include lines.
It probably works in main.cpp because some other header has this using line in it (or something similar).
Perhaps you wanted to #include<string>, not <string.h>. std::string also needs a namespace qualification, or an explicit using directive.
You must use std namespace. If this code in main.cpp you should write
using namespace std;
If this declaration is in header, then you shouldn't include namespace and just write
std::string level;
#include <string> would be the correct c++ include, also you need to specify the namespace with std::string or more generally with using namespace std;
This has been stumping me for a while. I am trying to create a function that takes a hash table, and returns said hash table. However I am getting this error in the header file,
error: ‘string’ was not declared in this scope.
error: template argument 1 is invalid
Here is the header file itself:
#ifndef NAME_SPAWN_H
#define NAME_SPAWN_H
#include <QString>
#include <QHash>
#include <string>
class Name_Spawn
{
public:
Name_Spawn();
void initalize();
private:
QString int_2_str(int);
void seed();
QHash<string,QString> setTable(QHash<string,QString>);
};
#endif // NAME_SPAWN_H
As you can see, string has been declared. Any ideas? I am at my wits end.
The real name of string is std::string. Try using that instead.
(You can leave off the std:: qualifier only if there's a using namespace std; directive in scope. But it's a good habit not to put using namespace std; in header files.)