I am taking a C++ class, and for some reason I can't get the classes in my header file to work on one of my programs. I am using Visual Studio 2017, and I added both my header file and my implementation file together with my test file by using the Solution Explorer in Visual Studio.
I have two constructors in my program, one default and one not. I tried deleting the second one from each file, and the program ran, but I can't get it to work otherwise.
Header File:
#pragma once
class BuckysClass {
public:
BuckysClass();
BuckysClass(string);
void coolSaying();
};
Implementation File:
#include "stdafx.h"
#include <iostream>
#include "BuckysClass.h"
#include <string>
using namespace std;
BuckysClass::BuckysClass() {
cout << "Bucky is ";
}
BuckysClass::BuckysClass(string x) {
cout << x;
}
void BuckysClass::coolSaying() {
cout << "preachin to the choir" << endl;
}
Test File:
#include "stdafx.h"
#include <iostream>
#include <string>
#include "BuckysClass.h"
using namespace std;
int main() {
BuckysClass buckysObject;
buckysObject.coolSaying();
BuckysClass buckysObject2("Bucky is not ");
buckysObject2.coolSaying();
system("Pause");
return 0;
}
syntax error:identifier 'string'
#include <string>//include the string header
class BuckysClass {
public:
BuckysClass();
BuckysClass(std::string);//add the namespace identifier
void coolSaying();
};
Your header file BuckysClass.h does not have the <string> Header file included.
Add:
#include<string>
using namespace std;
you should remove these includes from the corresponding .cpp file and just keep BuckysClass.h in cpp files.
Here are some more pictures of the test file and the error message I got that I couldn't post above.
Test File
Error Messages
Related
I'm having an issue where I am trying to use variables stored in a struct called from a header file. I am able to call to each cpp file ie., call an action from one to another however variables are proving difficult.
Client.cpp
#include <iostream>
#include "Header.h"
#include <vector>;
#include <conio.h>;
#include <string>;
using namespace std;
int main()
{
cout << "Please enter a name: " << flush;
cin >> sNam.sNames;
main2();
return 0;
}
Admin.cpp
#include <iostream>
#include "Header.h"
#include <vector>;
#include <conio.h>;
#include <string>;
using namespace std;
void main2()
{
if (sNam.sNames == sNam1.sNames1)
{
cout << "Correct Entry...\n";
}
else if (sNam.sNames != sNam1.sNames1)
{
cout << "Incorrect Entry...\n";
}
}
Header.h
#pragma once
#include "Admin.cpp"
#include "Client.cpp"
struct Names
{
string sNames;
string sNames1 = "John";
}sNam, sNam1;
don't include cpp files
also sNam and sNam1 are defined in both cpp files, you will have link time error
include Header.h in both Client.cpp and Admin.cpp
then declare what's common in Header.h
#pragma once
// #include "Admin.cpp"
// #include "Client.cpp"
struct Names
{
string sNames;
string sNames1 = "John";
};
extern Names sNam, sNam1;
void main2();
in one of cpp files
#include "Header.h"
Names sNam, sNam1;
When I'm writing a header file, the editor can't find <iostream>. And the std namespace can't be detected as well.
num.h
#include "tag.h"
#include "token.h"
#include <iostream>
#include <stdio.h>
#include <string>
#ifndef NUM_H_
#define NUM_H_
class Num : public Token {
public:
int value;
Num(int v) : Token(Tag::NUM) { value = v; };
std::string toString() {return " "+std::to_string(value);}
};
#endif
But the strange thing is, when I create a file that includes this header file, and write some demo code to test if this header file really works, then it works fine.
num.cpp
#include "num.h"
int main() {
Num n(1);
std::string a = n.toString();
std::cout<<a;
}
Here is what I get after running it:
I also find that it's fine to write #include <iostream> in the .cpp file, there won't be any error.
It's quite strange, I don't know what's wrong. Maybe it's a bug in VScode?
I have created a small c++ application with a header file, a cpp file, and a main function.
I expected that the header file would be an interface, that would just define my functions and properties, but the cpp file would be the one to implement.
However when I create my files, in my main function I have a breakpoint and try to step into my method, it just takes me straight to the header file definition instead of the implementing cpp file. Here is my code:
//header file
#pragma once
#include <string>
#include <SQLAPI.h>
class DbConnection
{
public:
int Id, Age;
std::string Name;
void ConnectToDatabase(const std::string databaseName) { }
void Retrieve(const std::string table) { }
};
Here is my cpp file:
#include "DbConnection.h"
#include <string>
#include <SQLAPI.h>
#include <iostream>
SAConnection sqlCon;
SACommand sqlCmd(&sqlCon);
int Id, Age;
std::string Name;
void ConnectToDatabase(const std::string databaseName)
{
sqlCon.Connect(
databaseName,
",
",
SA_SQLServer_Client);
}
void Retrieve(const std::string table)
{
//code to retrieve data
std::cout << "success";
}
My main function which is in a separate cpp file. I set a breakpoint and attempt to step into the method Retrieve and it takes me to the header file instead of the cpp. Any help with debugging is appreciated.
#include "DbConnection.h"
#include <stdio.h>
#include <iostream>
int main()
{
DbConnection con;
con.ConnectToDatabase(
"databaseParameter");
con.Retrieve("tableParameter");
return 0;
}
I assume it is one of two things, either my debugger in visual studio 2019 is not set to the proper setting, or it has to do with how my #include "DbConnection.h" statements are included in both cpp files.
Two things:
You have to write in the cpp file
void DbConnection::ConnectToDatabase(const std::string databaseName)
{
...
}
and so for the Retrieve function to tell the compiler that this is the function of the class.
You actually implemented the function in the header file when you put {} after the function declaration.
So I've done extensive googling and searching on StackOverflow and am unable to find a solution despite several answers with this exact issue.
I am trying to create a test class in an external file called Fpc5.cpp
It's contents are:
Fpc5.cpp
#include "stdafx.h"
#include "Fpc5.h";
#include <iostream>
using std::cout;
class Fpc5 {
int bar;
public:
void testMethod();
};
void Fpc5::testMethod() {
cout << "Hey it worked! ";
}
and my main .cpp file:
Test.cpp
// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
//#include "Fpc5.cpp"
#include "Fpc5.h";
using std::cout;
using std::cin;
using std::endl;
int main()
{
cout << "Hello" << endl;
Fpc5 testObj;
testObj.testMethod();
system("pause");
return 0;
}
all the answers I've read indicate this is caused becaused I used to be including the class in the main file itself which is why I created a header file
Fpc5.h
#pragma once
void testMethod();
This changed the error, but still did not fix the issue. Currently my Test.cpp does not recognize a Fpc5 class. I've also tried adding the Fpc5.cpp and Fpc5.h in stdafx.h and that still does not resolve the issue.
stdafx.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
//#include "Fpc5.cpp"
#include "Fpc5.h"
I'm sure this a simple syntax/conceptual understanding error, but I'm quite new to c++ and am not sure what is wrong.
This is definition of your class and it must be in Fpc5.h
class Fpc5 {
int bar;
public:
void testMethod();
};
Then, you have Fpc5.cpp where you implement methods of the class:
#include "Fpc5.h" // Compiler needs class definition to compile this file!
void Fpc5::testMethod()
{
}
And then you can use Fpc5 class in Test.cpp
#include "Fpc5.h"
int main()
{
Fpc5 foo;
foo.testMethod();
return 0;
}
As an alternative you can pack everything into Test.cpp
Move the definition of your class:
class Fpc5 {
int bar;
public:
void testMethod();
};
to the header file, "Fpc5.h".
Implement the methods to "Fpc5.cpp".
I am new C++ and I have a question about separate files classes in Xcode. I did write a program trying to lean class, but I got an error. Could anyone teach me how to do that right ?
I include the program that I tried below
this is the (main CCP):
#include <iostream>
#include "hhhhhhhhhhhhhhhh.h"
using namespace std;
int main ( int argc, char ** argv )
{
bassam bo;
bo.bassamfunction();
}
this is the (.h) :
#ifndef __try_some_concspte__hhhhhhhhhhhhhhhh__
#define __try_some_concspte__hhhhhhhhhhhhhhhh__
#include <iostream>
class bassam{
public:
void bassamfunction();
};
#endif /* defined(__try_some_concspte__hhhhhhhhhhhhhhhh__) */
this is the (CCP):
#include <iostream>
#include "hhhhhhhhhhhhhhhh.h"
using namespace std;
bassam::bassamfunction()
{
cout << " heloo I am here "<< endl ;
}
Your problem is that bassam::bassamfunction() has no return type in the .cpp file. This should be changed to void bassam::bassamfunction().