Use of undeclared identifier in header file (Clang) - c++

I am creating a function to read the contents of a file, located in an IO.cpp file:
#include "IO.h"
#include <iostream>
#include <fstream>
IO::IO()
{
//ctor
}
void IO::readFile(std::string fileName)
{
std::ofstream inputFile;
inputFile.open(FileName);
inputFile >> fileName.toStdString;
inputFile.close();
std::cout << fileName;
}
With the header file IO.h:
#ifndef IO_H
#define IO_H
class IO
{
public:
IO();
void readFile(std::string inputFile);
protected:
private:
};
#endif // IO_H
But I get an error from Clang that says
include/IO.h|9|error: use of undeclared identifier 'std'|
And I can't figure out how to solve it.

When parsing the header (specifically the void readFile(std::string inputFile); line), the compiler doesn't know an std namespace exists, much less string exists inside that namespace.
#include <string> in the header.

In many cases this occurs when you forget to include #include <iostream> on implementation of methods on a separate file i.e. outside main function (i.e. when you go header file route)

Related

Visual Studio 2019 debugger steps into header file instead of cpp

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.

C++:declaring a function that returns string in header file?

In one of my modules, I have a function (changeNum) that returns a string and accepts a parameter that is a string. I tried to declare this function in my header file as following:
std::string changeNum(std::string s);
[and I included the string header file into the header file as well]
but I'm still getting the following error in my header file: "unknown type name 'string'" What do I do?
Here is the whole code:
My header file is the following:
#pragma once
#include <string>
std::string changeNum(std::string s);
My module with the function changeNum is defined as the following
#include <string>
string changeNum(string s){
return s;
}
Try it:
Header.h
#pragma once
#include <string>
std::string changeNum(std::string s);
Source.cpp
#include "Header.h"
std::string changeNum(std::string s) {
return s;
}
main.cpp
#include "Header.h"
#include <iostream>
int main()
{
std::string sample_str = changeNum("Hello");
std::cout << sample_str.c_str();
}
Tested on VS and removed #include "pch.h" from aforementioned code.

Why am I getting a "declaration is incompatible with (x)"?

I'm creating a class called person right now in separate header and cpp files.
And for one of the functions I'm getting this error:
declaration is incompatible with "Person::stat Person::getStat()" (declared at line 26 of "C:...")
(Not the exact directory but you get the idea)
Here is the code in the header file:
#pragma once
#include <string>
#include <iostream>
class Person
{
public:
struct stat {
int str;
int end;
int dex;
int intel;
};
Person();
~Person();
//properties
stat getStat();
};
Here is the code in the cpp file:
#include "pch.h"
#include "Person.h"
#include <string>
#include <iostream>
Person::Person()
:age(12), height(0)
{
}
Person::~Person()
{
}
struct stat Person::getStat() {
}
I'm getting the error with the getStat() function. I've tried including the string and iostream headers in both file and also only in the header file since a similar post suggested it. Both didn't solve my problem however.
Should be
Person::stat Person::getStat() {
}
Your version declares a new struct stat which isn't the same as Person::stat.
struct stat Person::getStat() is a method that returns a stat that belongs to the global namespace, not to Person:
Person::stat Person::getStat()
Note that there is no struct here (to avoid declaring one). In C++, we don't use struct after the type has been declared.

Including header files in C++ (class definition and method implementation)

I have already checked StackOverflow to find the solution to my problem, but I think I might be missing something. I am trying to define a class in a header file (.h) and implement its methods in a cpp file (.cpp), but it does not work.
main.cpp:
#include <iostream>
#include "Message.h"
using namespace std;
int main()
{
Message *t = new (Message);
t->display();
return 0;
}
Message.h:
#ifndef MESSAGE_H_INCLUDED
#define MESSAGE_H_INCLUDED
class Message {
public:
void display();
};
#endif // MESSAGE_H_INCLUDED
Message.cpp:
#include "Message.h"
void Message::display() {
cout << "Hello!";
}
I don't understand why I keep getting the following error
undefined reference to 'Message::display()'
Compile this with the command g++ -std=c++11 Message.cpp main.cpp

error C2146: syntax error : missing ';' before identifier

i can't get rid of these errors... i have semicolons everywhere i checked...
the code is simple:
the error takes me to the definition "string name" in article.h...
main.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
#include "article.h"
int main()
{
string si;
char article[128];
vector<Article> articles;
ifstream file;
file.open("input.txt",ifstream::in);
while(!file.eof())
{
file.getline(article,128);
articles.push_back(Article(article));
}
file.close();
while(1);
return(1);
}
article.h:
#ifndef Article_H
#define Article_H
class Article
{
public:
int year;
string name;
Article(char *i_name);
};
#endif
You should add:
#include <string>
to your "article.h" header file and declare name like this:
std::string name;
It seems the string type is not defined in the artivle.h file. Try to include iostream and add using namespace std (or write std::string instead of using namespace)
You should use the std:: namespace prefix in the header, like
std::string name;