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>
Related
I am trying to create a AppServiceConnection but when doing so I get a Incomplete Type not allowed
I verified the header file is imported for that class as mention in other stackoverflow questions.
I tried several different attempted to define the AppServieConnection. Am I putting in the wrong place?
The only way it worked was putting it above the main method.
Here is my code
#include "pch.h"
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <windows.h>
#include <winrt/Windows.ApplicationModel.Activation.h>
#include <tchar.h>
#include <stdio.h>
#include <ppltasks.h>
#include <appmodel.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <ppltasks.h>
#include <windows.h>
#include <appmodel.h>
#include <malloc.h>
#include <stdio.h>
#include <assert.h>
using namespace winrt;
using namespace concurrency;
using namespace Windows::Foundation;
using namespace std;
using namespace Windows::ApplicationModel::AppService;
AppServiceConnection connection_worked; // defining it here works but cannot call any methods from it
int main()
{
init_apartment();
Uri uri(L"http://aka.ms/cppwinrt");
printf("Hello, %ls!\n", uri.AbsoluteUri().c_str());
}
class NewClass {
private:
Windows::ApplicationModel::AppService::AppServiceConnection connection{nullptr};
Windows::ApplicationModel::AppService::AppServiceConnection connection2;
Windows::ApplicationModel::AppService::AppServiceConnection connection3 = nullptr;
AppServiceConnection connection4;
AppServiceConnection connection5 = nullptr;
AppServiceConnection connection6 = AppServiceConnection();
IAsyncAction InitializeAppServiceConnection() {
}
public:
NewClass() {
}
};
The error is clear, you are missing AppServiceConnection header file.
Please add the header file
#include <winrt/Windows.ApplicationModel.AppService.h>
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
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");
How I can sent string variable into function in other file?
main.cpp:
#include <iostream>
#include <conio.h>
#include <string>
#include "headers.hpp"
using namespace std;
int main()
{
string a;
cout<<"Type:"<<endl;
cin>>a;
other(a);
getch();
return( 0 );
}
headers.hpp:
#ifndef HEADERS_HPP
#define HEADERS_HPP
void other(string a);
#endif
function.cpp:
#include "headers.hpp"
#include <iostream>
#include <math.h>
using namespace std;
void other(string a){
cout<<a;}
I don't know why it doesn't work. Do you know solution?
If you are saying it does not compile, you have to move the #include <string> from main.cpp into headers.hpp
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.