I have added the errors within comment lines of the code for the highlighted lines by the compiler.
header file:
#ifndef ADDRESS_H_EXISTS
#define ADDRESS_H_EXISTS
#include <iostream>
#include <string>
using namespace std;
class Address{
private:
string address1;
string address2;
string city;
string state;
string zipCode;
public:
Address(){} //note: 'Address::Address()' previously defined here|
Address(
const string &address1,
const string &address2,
const string &city,
const string &state,
const string &zipCode
){}
NOTE: #endif exists at the end of header file
source file:
#include <iostream>
#include <string>
#include "address.h"
using namespace std;
Address::Address(){} // error: redefinition of 'Address::Address()'
Address::Address( // error: redefinition of 'Address::Address(const string&,
// const string&, const string&, const string&, const string&)'|
const string &address1,
const string &address2,
const string &city,
const string &state,
const string &zipCode
):
address1(address1),
address2(address2),
city(city),
state(state),
zipCode(zipCode)
{
Address::address1 = address1_c;
Address::address2 = address2_c;
Address::city = city_c;
Address::state = state_c;
Address::zipCode = zip_c;
}
All of the most popular questions about this error concluded that header guards were needed, although, there are guards already included in this code.
I thought I was misunderstanding how to properly separate the initialization list between header and source files but when I commented that out it was still producing the same error.
What you're typically supposed to do is define the function prototypes in the header file, and the function definition in the source file. However, in your header file, you seem to have specified a definition of the function already using the empty {} brackets. Hence the compiler is complaining that you've redefined the function definition in the source file. If you remove those two {} empty blocks in the header file and replace them with a semicolon ;, it should solve this error.
Basically, it should look like this in your header:
Address();
Address(
const string &address1,
const string &address2,
const string &city,
const string &state,
const string &zipCode
);
You are getting redefinition errors ( which i think are linker errors, and not compile errors ) because Address::Address() and Address::Address(const string&, const string&, const string&, const string&, const string&) are already defined in the header file, and you define them again in the CPP file
To avoid that, you need to replace function definition by declarations in your header file, by replacing {} by ; in your header file, this way :
public:
Address(); //By replacing '{}' by ';', you change that function definition into a function DECLARATION
Address(
const string &address1,
const string &address2,
const string &city,
const string &state,
const string &zipCode
); // Same for here
At the exception of inline and template functions, function declaration goes in header file, and definitions goes into the CPP file
Related
I'm attempting to compile my code with g++, but its throwing this compiler error:
Enrollment.h:3:7: error: redefinition of class sict::Enrollment
Enrollment.h:3:7: error: previous definition of class sict::Enrollment
my Enrollment.h:
namespace sict{
class Enrollment{
private:
char _name[31];
char _code[11];
int _year;
int _semester;
int _slot;
bool _enrolled;
public:
Enrollment(const char* name , const char* code, int year, int semester , int time );
Enrollment();
void set(const char* , const char* , int ,int, int , bool = false );
void display(bool nameOnly = false)const;
bool valid()const;
void setEmpty();
bool isEnrolled() const;
bool hasConflict(const Enrollment &other) const;
};
}
Any way to fix this?
The problem is likely that your header file is included (directly and indirectly) in the same translation unit. You should use some way of avoiding the multiple includes of the same header file in your cpp's. I prefer #pragma once in the beginning of your header file - it is not standard but it is supported by all major compilers. Otherwise you can go for good-old include guards:
#ifndef _Enrollment_h_
#define _Enrollment_h_
// Your header contents here
#endif
or with pragma:
#pragma once
// Your header contents here
You need to use some include guards. Either #pragma once or:
#ifndef MY_FILE_H
#define MY_FILE_H
....
#endif //MY_FILE_H
This is to prevent the same code being included in every file where you include this header (double inclusion). This will essentially help out the pre-processor. More information here.
I have the following code:
//test.cpp
#include <Physical_file.h>
#include <boost\python.hpp>
using namespace boost::python;
using namespace FMS_Physical;
BOOST_PYTHON_MODULE(python_bridge)
{
class_<Physical_file, boost::noncopyable>("pf")
.def(init<const string&, optional<int, const string&>>())
;
}
//Physical_file.h
#include <fstream>
#include "tools.h"
using namespace std;
namespace FMS_Physical
{
class Physical_file
{
public:
fstream filefl;
string workingDir;
string fileName;
int fileSize;
block currBlock;
block FHBuffer;
bool opened;
string openMode;
/************
functions
************/
Physical_file(void);
Physical_file(const string &FileName, int FileSize,const string &Dir = getCurrentPath());
Physical_file(const string &FileName, const string &Type, const string &Dir = getCurrentPath());
~Physical_file(void);
};
}
there is some more code, I think it's irrelevant to the question.
when I try to compile the code, I get the following error:
Error 5 error C2664: 'FMS_Physical::Physical_file::Physical_file(const FMS_Physical::Physical_file &)' : cannot convert parameter 1 from 'const std::string' to 'const FMS_Physical::Physical_file &'
when I delete the optional from the definition of the constructor (in test.cpp) the error disappears, but I don't get the optional parameters.
I'm compiling using VS2010, python27 and c++ boost libs.
Can any one explain why I get this error and how can I solve it?
EDIT
I tried exposing the third constructor instead using the following line:
.def(init<const string&, const string &, optional<const string &>>())
this didn't cause any errors.
You have forgotten to give a default to FileSize.
For this declaration to work: init<const string&, optional<int, const string&>>
you need a constructor that can be called with only a const string& parameter. You do not have such at the moment, which is the exact complaint of the compiler.
I am getting this error when I compile with GCC:
error: declaration of 'static int utils::StringUtils::SplitString(const std::string&, const std::string&, std::vector<std::basic_string<char> >&, bool)' outside of class is not definition
Code:
Header:
namespace utils
{
/*
* This class provides static String utilities based on STL library.
*/
class StringUtils
{
public:
/**
* Splits the string based on the given delimiter.
* Reference: http://www.codeproject.com/Articles/1114/STL-Split-String
*/
static int SplitString( const std::string& input,
const std::string& delimiter,
std::vector<std::string>& results,
bool includeEmpties = true );
};
};
Source:
namespace utils
{
int StringUtils::SplitString( const std::string& input,
const std::string& delimiter,
std::vector<std::string>& results,
bool includeEmpties );
{
....
}
}
Take the semi-colon off the end of the definition in your source file! Copy-paste error =)
I believe you need to lose that semicolon in your source file. Should be:
namespace utils
{
int StringUtils::SplitString( const std::string& input,
const std::string& delimiter,
std::vector<std::string>& results,
bool includeEmpties ) // <--- No more semi-colon!
{
....
}
}
I try to make class where I can creat new object of Student.
I've got some problem with definition of class body (student.cpp) and class (student.h).
Error:
In file included from student.cpp:1:
student.h:21:7: warning: no newline at end of file
student.cpp:6: error: prototype for `Student::Student()' does not match any in class `Student'
student.h:6: error: candidates are: Student::Student(const Student&)
student.h:8: error: Student::Student(char*, char*, char*, char*, int, int, bool)
student.cpp
//body definition
#include "student.h"
#include <iostream>
Student::Student()
{
m_imie = "0";
m_nazwisko = "0";
m_pesel = "0";
m_indeks = "0";
m_wiek = 0;
m_semestr = 0;
m_plec = false;
}
student.h
//class definition without body
#include <string.h>
class Student {
//konstruktor domyslny
Student (char* imie, char* nazwisko, char* pesel, char* indeks, int wiek, int semestr, bool plec):
m_imie(imie), m_nazwisko(nazwisko), m_pesel(pesel), m_indeks(indeks), m_wiek(wiek), m_semestr(semestr), m_plec(plec)
{}
private:
char* m_imie;
char* m_nazwisko;
char* m_pesel;
char* m_indeks;
int m_wiek;
int m_semestr;
bool m_plec;
};
Your constructor in cpp file does not match constructor in header.
Every constructors/desctructors/methods realizations in cpp should be first defined in class in header.
If you want to have 2 constructors - 1 with no parameters and one with many parameters as you have. You need to add definition of your constructor in header.
//class definition without body
#include <string.h>
class Student {
//konstruktor domyslny
Student (char* imie, char* nazwisko, char* pesel, char* indeks, int wiek, int semestr, bool plec):
m_imie(imie), m_nazwisko(nazwisko), m_pesel(pesel), m_indeks(indeks), m_wiek(wiek), m_semestr(semestr), m_plec(plec)
{} //here really implementation made
Student(); //one more constructor without impementation
private:
char* m_imie;
char* m_nazwisko;
char* m_pesel;
char* m_indeks;
int m_wiek;
int m_semestr;
bool m_plec;
};
In you header file you declare that Student has just one constructor with all the written parameters but no default Student() constructor, you should add it to header:
class Student {
Student();
Student(char* imie, char* nazwisko ... ) {}
};
You wrote a body for a Student constructor that doesn't take any parameters:
Student::Student( /* NO PARAMETERS */ )
But this function, Student(), is not in the class-definition.
This generates the error:
prototype for `Student::Student()' does not match any in class `Student'
You need to write:
class Student {
public:
Student(); /* NOW it is declared as well as defined */
[... all the other stuff ...]
};
Now, there is both a prototype for Student() and also for Student(/* 7 parameters */)
The fix for the other error is simple:
student.h:21:7: warning: no newline at end of file
The fix is to put a newline at the end of the file! :-)
I am trying to Compile in Visual C++ and just added this config file loader/parser to my project. For some ever function defined in class CProfileData is receiving at least one of two errors:
missing type specifier - int assumed.
syntax error : missing ',' before '&'
When obviously this should just be a referenced string
#ifdef UVSS_EXPORTS
#define UVSS_API __declspec(dllexport)
#else
#define UVSS_API __declspec(dllimport)
#endif
class CProfileData
{
public:
UVSS_API CProfileData(){};
UVSS_API CProfileData(const string& profileFile);
UVSS_API ~CProfileData(void);
UVSS_API bool GetVariable( const string& sectionName, const string& variableName, string& valueRet );
UVSS_API bool GetSection( const string& sectionName, SECTION_MAP **pMapRet );
UVSS_API bool GetVariableW( const string& sectionName, const string& variableName, wstring& valueRet );
UVSS_API bool GetVariableInt( const string& sectionName, const string& variableName, int *pIntRet );
private:
void ToLower( string& str );
void TrimWhitespace( string& str);
bool IsComment( const string& str );
bool IsSection( const string& str, string& secName );
bool IsVariable( const string& str, string& name, string& value );
PROFILE_MAP m_mapProfile;
};
Include <string>:
#include <string>
And write std::string wherever you've written string.
Its not a good idea to do either of the following in a header file:
using namespace std; //avoid doing this
using std::string; //avoid doing this as well
Ensure that these two lines appear before including this header:
#include <string>
using std::string;