error C2146: syntax error : missing ';' before identifier - c++

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;

Related

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.

Redefining function in header file c++

After writing my header file and trying to use it in the cpp.file. The compiler gives me an error when trying to redefine the function in header file.
I didn't face this problem the previous times I was using headers in a similar way. Maybe I initialize the Vector in a wrong way. Anyways here is the code:
#include <string>
#include <vector>
#include "lajitellut.h"
using namespace std;
namespace otecpp_lajitellut{
/*this is where the error appears*/
vector<string> lajitellut(int lkm, char*mjt[]){
vector<string> stringVector;
for(int i =0; i<lkm; i++){
stringVector.push_back(mjt[i]);
}
for(int i =0; i<lkm; i++){
for(int a = 0; a<lkm;a++){
if(stringVector[i] < stringVector[a]){
stringVector[i].swap(stringVector[a]);
}
}
}
return stringVector;
}
}
And here is the header file
#ifndef kissa
#define kissa
#include <string>
#include <vector>
namespace otecpp_lajitellut{
std::vector <std::string> lajitellut(int lkm, char* mjt[]) {
std::vector<std::string> stringVector;
return stringVector;
}
}
#endif // kissa
Put only the function declaration in the "lajitellut.h" header file:
#include <vector>
#include <string>
namespace otecpp_lajitellut {
std::vector<std::string> lajitellut(int, char*);
}
Put the function definition in the source "*.cpp" file:
#include <iostream>
#include <vector>
#include <string>
#include "lajitellut.h"
namespace otecpp_lajitellut {
std::vector<std::string> lajitellut(int lkm, char* mjt[]) {
// your code in here
}
}
int main(){
auto a = otecpp_lajitellut::lajitellut(10, "asd");
}
Note that definition is also a declaration. That being said you don't have a vector there. You have a function of type std::vector<std::string>. Don't use using namespace std;.
Ron is right.
Your function lajitellut() is already implemented in the .h file with the same signature. You can not create a double in the same namespace.
You can change the arguments or the type of the return value or change the namespace in the .cpp file.

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

String not working in visual studio c++ [duplicate]

I have this class defined but it doesn't work at all.
#ifndef LIBROS_H
#define LIBROS_H
#include "Articulo.h"
class Libros: public Articulo
{
public:
Libros();
~Libros();
string Autor;
string Editorial;
void mostrar();
void llenar();
};
# endif
this gives:
error
C4430: missing type specifier - int assumed. Note:C++ does not support default-int
You forgot to #include the right header.
#include <string>
And since you have no using statement, you'll need to qualify your strings with the namespace they're in, which is std:
std::string Autor;
std::string Editorial;
Two things:
#include <string>
and the string is in the std namespace. You'll need to use std::string rather than string.
You have to include the string header and you must either prefix string with the namespace std or use a using namespace std;
#ifndef LIBROS_H
#define LIBROS_H
#include <string>
#include "Articulo.h"
class Libros: public Articulo
{
public:
Libros();
~Libros();
std::string Autor;
std::string Editorial;
void mostrar();
void llenar();
};
# endif

Global function header and implementation

how can I divide the header and implementation of a global function?
My way is:
split.h
#pragma once
#include <string>
#include <vector>
#include <functional>
#include <iostream>
void split(const string s, const string c);
split.cpp
#include "split.h"
void split(const string& s, const string& c){
...
}
main.cpp
// main.cpp : Defines the entry point for the console application.
//
#include <string>
#include <vector>
#include <functional>
#include <iostream>
#include "stdafx.h"
#include "split.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<string> v;
string s = "The;;woraaald;;is;;not;;enoaaaugh";
string c = " aaa ;; ccc";
split(s,c);
return 0;
}
And errors are:
Error 1 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int ...\split.h 8
Error 2 error C2146: syntax error : missing ',' before identifier 's' ...\split.h 8
How can I solve this problem? thx
In header file use std:: namespace qualifier - std::string
In the header file, you have to give the fully qualified name std::string. In the source files, you can add using namespace std; or using std::string; and then just spell it string.
Also, you've declared the function taking arguments by value, but defined it taking arguments by reference.
At least one problem is, you are missing the 'std::' namespace qualifier in split.h:
#pragma once
#include <string>
#include <vector>
#include <functional>
#include <iostream>
void split(const std::string s, const std::string c);
I think you either forgot to put using std::string; before split declaration or use std::string const& as split parameter declarations.
Also split declaration mismatch from split definition string const vs string const&