is implicitly deleted because the default definition would be ill-formed: - c++

I am creating a project that reads a file and does some things with the data within the file:
NameSurferDataBase.cpp
#ifndef NAMESURFERDATABASE_CPP
#define NAMESURFERDATABASE_CPP
#include "NameSurferDataBase.h"
#include "linked_list.h"
#include <iostream>
#include <list>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <ostream>
#include <sstream>
#include <iomanip>
#include <istream>
#include <cstdio>
using namespace std;
NameSurferDataBase::NameSurferDataBase(string filename){
getNameData(filename);
}
void NameSurferDataBase::getNameData(string filename){
ifstream input;
input.open(filename);
if(!input.is_open()){
cout << "Not Open";
}else{
string temp;
while(input.is_good()){
getline(input,temp);
if(!input.eof()){
NameSurferEntry entry(temp);
database.InsertInOrder(entry);
}
}
}
}
NameSurferEntry NameSurferDataBase::findEntry(string name){
NameSurferEntry temp(name);
if(database.Search(temp) == true){
return temp;
} else{
cout << "Name not found" << endl;
}
}
#endif
NameSurferDataBase.h
#ifndef NAMESURFERDATABASE_H
#define NAMESURFERDATABASE_H
#include "NameSurferEntry.h"
#include "linked_list.h"
#include <iostream>
#include <list>
#include <string>
class NameSurferDataBase {
public:
NameSurferDataBase(string filename);
void getNameData(string filename);
NameSurferEntry findEntry(string name);
private:
linked_list<NameSurferEntry> database;
};
#endif
Whenever I try to compile, I get the below errors on this line in my main code:
NameSurferDataBase namesdb = NameSurferDataBase("NamesData.txt");
main.cpp: In function ‘int main()’:
main.cpp:19:67: error: use of deleted function ‘NameSurferDataBase::NameSurferDataBase(NameSurferDataBase&&)’
NameSurferDataBase namesdb = NameSurferDataBase("NamesData.txt");
^
In file included from main.cpp:5:0:
NameSurferDataBase.h:11:7: note: ‘NameSurferDataBase::NameSurferDataBase(NameSurferDataBase&&)’ is implicitly deleted because the default definition would be ill-formed:
class NameSurferDataBase {
^~~~~~~~~~~~~~~~~~
NameSurferDataBase.h:11:7: error: invalid initialization of non-const reference of type ‘linked_list<NameSurferEntry>&’ from an rvalue of type ‘linked_list<NameSurferEntry>’
Why is it saying my constructor is ill-formed?

You are constructing a temporary NameSurferDataBase and moving it into namesdb. A default move constructor cannot be generated due to your use of linked_list, which likely prohibits moving.
Construct namesdb directly and it should work, e.g:
NameSurferDataBase namesdb("NamesData.txt");

Related

Referee.cpp:10:18: error: no matching function for call to ‘Human::Human()’ Referee::Referee()

I'm new to c++ and was having problem with doing some inheritance, does any one know why im getting this error? (its the only error I'm getting when compiling).
I compiled with g++ -o. Also I'm so sorry in advance if I'm doing a lot of things wrong here, I'm very new to c++. >_<
Please let me know how I can make my code better or more efficient.
computer.h
#ifndef RPS_H
#define RPS_H
#include <iostream>
#include <string>
#include <stdio.h>
class Computer
{
public:
Computer(std::string);
~Computer();
char charc;
};
#endif
human.h
#ifndef HUMAN_H
#define HUMAN_H
#include <iostream>
#include <string>
#include <stdio.h>
class Human
{
public:
Human(std::string);
~Human();
char charh;
};
#endif
referee.h
#ifndef REFEREE_H
#define REFEREE_H
#include <iostream>
#include <string>
#include <stdio.h>
#include "human.h"
class Referee : public Human{
public:
Referee();
~Referee();
bool Winneris();
};
#endif
Computer.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "computer.h"
using namespace std;
Computer::Computer(string char_c)
{
}
Computer::~Computer()
{
}
Human.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "human.h"
using namespace std;
Human::Human(string char_h){
char_h=charh;
cout<<"r/p/s?"<<endl;
cin>>charh;
}
Human::~Human()
{
}
Referee.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "referee.h"
using namespace std;
Referee::Referee(){
}
bool Referee::Winneris(){
if (charh=='r'){
cout<<"draw"<<endl;
}
else if(charh=='p'){
cout<<"Victory!"<<endl;
}
else if(charh=='s')
{
cout<<"Defeat"<<endl;
}
return 0;
}
Referee::~ReReferee(){
}
main.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "human.h"
#include "computer.h"
#include "referee.h"
using namespace std;
string char_h;
string char_c;
// main program
int main()
{
Human *round1h;
round1h = new Human(char_h);
Computer *round1c;
round1c = new Computer(char_c);
Referee *round1r;
round1r = new Referee();
round1r -> Winneris();
}
When you have written the parameterized constructor in respective classes. You have created the class objects, which call the default constructor which takes no parameter.
You have to define the default constructor as well in your respective classes.
Human::Human()
{}
Computer::Computer()
{}
Referee::Referee()
{}
Constructor types

C++ Multiple Definition [Error]

Error: multiple definition of `GameKey::getGameKeywords()'
GameKey.cpp and .h cause error, while ExitKey.cpp and .h are essentially the exact same class and header but do not produce an error.
(I know the whole thing about using namespace std)
//Function Declarations
#ifndef GAMEKEY_H
#define GAMEKEY_H
// C++ libraries
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
class GameKey
{
private:
string keyString;
string lineData;
public:
// Default constructor
GameKey();
// Deconstructor
~GameKey();
// Get keywords
string getGameKeywords();
};
#endif
GameKey.cpp
//Function Definitions
#include "GameKey.h"
// Constructor
GameKey::GameKey()
{
}
// Deconstructor
GameKey::~GameKey()
{
}
// Get keywords
string GameKey::getGameKeywords()
{
ifstream infile;
infile.open("GameKey.txt");
while (getline(infile, lineData))
{
keyString.append(lineData);
keyString.append("\n");
}
infile.close();
return keyString;
}
ExitKey.h
//Function Declarations
#ifndef EXITKEY_H
#define EXITKEY_H
// C++ libraries
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
class ExitKey
{
private:
string keyString;
string lineData;
public:
// Default constructor
ExitKey();
// Deconstructor
~ExitKey();
// Get keywords
string getExitKeywords();
};
#endif
ExitKey.cpp
//Function Definitions
#include "ExitKey.h"
// Constructor
ExitKey::ExitKey()
{
}
// Deconstructor
ExitKey::~ExitKey()
{
}
// Get keywords
string ExitKey::getExitKeywords()
{
ifstream infile;
infile.open("ExitKey.txt");
while (getline(infile, lineData))
{
keyString.append(lineData);
keyString.append("\n");
}
infile.close();
return keyString;
}
Thanks for any help!
I think you probably include GameKey.cpp instead of GameKey.h elsewhere
I am not certain as the command used for compilation is not posted.
One possibility is repeating the file names in your compilation command could also lead to this error.
for example :-
g++ ExitKey.cpp GameKey.cpp GameKey.cpp main.cpp -o main

Sharing a defined global variable in a header amongst two cpp files

I am interested in being able to share a defined global variable across two cpp files. Is the following possible? I am interested in this to avoid having to initialize the global shared variable multiple times. I am having trouble being able to build this code. How would you recommend to declare/define myMap in this case?
MapHeader.h
#ifndef _MAP_HEADER_
#define _MAP_HEADER_
#include <string>
#include <map>
using namespace std;
extern const map<int, string> myMap = {{100 , "one hundred"}, {200,"two hundred"}, {300,"three hundred"}};
#endif // _MAP_HEADER_
FirstFile.h
#ifndef _FIRST_FILE_
#define _FIRST_FILE_
#include "MapHeader.h"
#include <iostream>
#include <string>
using namespace std;
void myFunction1();
#endif
FirstFile.cpp
#include "FirstFile.h"
void myFunction1(){
cout << "myFunction1()" << myMap[100] << endl;
}
SecondFile.h
#ifndef _SECOND_FILE_
#define _SECOND_FILE_
#include "MapHeader.h"
#include <iostream>
#include <string>
using namespace std;
void myFunction2();
#endif
SecondFile.cpp
#include "SecondFile.h"
void myFunction2(){
cout << "myFunction2()" << myMap[200] << endl;
}
Main.cpp
#include "FirstFile.h"
#include "SecondFile.h"
int main()
{
myFunction1();
myFunction2();
return 0;
}
I am getting the error message:
error: passing 'const std::map<>' as 'this' argument of 'std:map<>' .....
In MapHeader.h, change your definition to extern map myMap; and then move your definition exactly as you had it in MapHeader.h into one of the .cpp's.

"Was not declared in this scope" error struct definition. c++

I'm thinking I have angered the "Header Guard" gods, but I don't see where. My program is laid out as follows:
(note :this is just the relevant info on these files)
main file:
#include "playlist.h"
#include "playlistitem.h"
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main (int argc, char** argv)
//snip
PlayList allSongs;
//snip
playist.h:
#ifndef PLAYLIST_H
#define PLAYLIST_H
#include <iostream>
#include <string>
#include <vector>
#include "playlistitem.h"
#include "song.h"
#include "time.h"
struct Playlist {
std::vector<Song> songs;
Time cdTotalTime;
int totalTime;
};
plalist.cpp:
#include <iostream>
#include <string>
#include <vector>
#include "playlist.h"
song.h:
#ifndef SONG_H
#define SONG_H
#include <iostream>
#include <string>
#include "time.h"
struct Song {
std::string title;
std::string artist;
std::string album;
int track;
Time length;
};
song.cpp:
#include "song.h"
#include "csv.h"
#include <sstream>
#include <vector>
I get "Playlist was not declared in this scope" on line:
PlayList allSongs;
In my main file.
Thanks!
Check your capitalization.
Playlist and PlayList are being used.
You've just got your capitalization wrong... it's declared as Playlist, used as PlayList
clang's spell checking is helpful for this type of thing.
tmp.cpp:5:1: error: unknown type name 'PlayList'; did you mean 'Playlist'?
PlayList pl;
^~~~~~~~
Playlist
tmp.cpp:1:8: note: 'Playlist' declared here
struct Playlist {
^
1 error generated.

What's wrong with this C++ program?

I just have an header file and and an .cpp file i am just passing an value to function but it gives me an error
main.c
#include "me.h"
#include <iostream>
#include <sstream>
#include <string.h>
using namespace std;
int main()
{
me("http");
}
me.h
#ifndef ME_H_
#define ME_H_
#include <string.h>
class me {
public:
me(std::string u);
virtual ~me();
};
#endif /* ME_H_ */
me.cpp
#include "me.h"
#include <iostream>
#include <string.h>
using namespace std;
me::me(std::string u) {
// TODO Auto-generated constructor stub
cout << "help";
}
me::~me() {
// TODO Auto-generated destructor stub
}
I am getting an error
In file included from ../src/me.cpp:8:
../src/me.h:13: error: expected ‘)’ before ‘u’
../src/me.cpp:12: error: prototype for ‘me::me(std::string)’ does not match any in class ‘me’
../src/me.h:11: error: candidates are: me::me(const me&)
../src/me.h:11: error: me::me()
make: *** [src/me.o] Error 1
#include <string> instead of #include <string.h>
string.h is the C string header, accessible in C++ as <cstring>
<string> is the C++ header that defines std::string
you want #include <string> instead of #include <string.h>