C++ warning: pragma once in main file - c++

I'm have an issue with compiling my program with g++ 8.3. I have approx 10 classes in a program. These classes are placed in header files and their full definitions are in .cpp files. I'm including these classes the same way as in this code:
main.cpp:
#include "CPerson.h"
int main()
{
CPerson person1(10 , "Peter");
CPerson person2(20 , "James");
person1.Print();
person2.Print();
return 0;
}
CPerson.h:
#pragma once
#include <iostream>
#include <string>
using namespace std;
class CPerson{
protected:
int m_Age;
string m_Name;
public:
CPerson( const int age , const char * name ) :
m_Age(age), m_Name(name){}
void Print(){
cout << "Hello Im " << m_Name << " - " << m_Age << "years old" << endl;
}
};
When I try to compile this C++ program with the following command:
g++ main.cpp CPerson.h
I get this message:
warning: #pragma once in main file
Is here anything I can do about this, or is it just bug in the g++ compiler?
SOLVED:
You need to compile only .cpp files with declarations of methods of each class thats defined in Class.h

You get the warning because you are compiling a file that contains #pragma once. #pragma once is only intended to be used in headers, and there is no need to compile headers; hence the warning. Solution: Don't compile headers.

Related

Static Class Bool Stating Multiple Definitions Across Files [duplicate]

I'm writing some code where I need to have a class variable that's a static int array. I understand that I can do this with something like this in the header file, A.h:
#ifndef A_H_
#define A_H_
class A
{
public:
static const int a[];
};
const int A::a[] = {1,2};
#endif
This works just fine if I'm then including this header in only one other file, something like the following, main.cpp:
#include "A.h"
#include <iostream>
using namespace std;
int main()
{
A myA;
cout << "0: " << myA.a[0] << endl;
cout << "1: " << myA.a[1] << endl;
}
But suppose I need my class A to be a bit more complicated, and I want to have an A.cpp file as well. I'll keep my main.cpp file the same, but then change A.h as follows (I've just added a function, printA):
#ifndef A_H_
#define A_H_
class A
{
public:
void printA() const;
static const int a[];
};
const int A::a[] = {1,2};
#endif
And then in file A.cpp:
#include "A.h"
#include <iostream>
using namespace std;
void A::printA() const
{
cout << "Printing in A.cpp." << endl;
cout << "A.0: " << a[0] << endl;
cout << "A.1: " << a[1] << endl;
}
Compiling A.o with gcc -o A.o -c A.cpp is fine. But linking this when compiling main.cpp (gcc -o atest main.cpp A.o) fails with "multiple definition of `A::a'".
I've been scouring the internet for solutions, and found people who have variables declared in their headers who get the "multiple definition" error when they include the header in multiple places, and the solution seems to be to declare the variable extern in the header, then define it in only one source (non-header) source file. But I can't declare a class variable both static and extern, can I? If I try, or if I just declare it extern, I get a warning about the variable not being static (also a "conflicting specifiers" error when I try both).
So, my question: is it possible to have static array class variables in the case that the header file needs to be included in more than one source file? If so, how?
You're violating the one definition rule. Move the definition inside an implementation file:
//A.cpp
#include "A.h"
const int A::a[] = {1,2};
The solution you are reffering to, with extern, applies to non-member variables. In your case a is a class member.
You should remove the "const int A::a[] = {1,2};" line from the header file. Put this definition line in one of you .cpp files.
Then you can include the header file several times where you need it.

static const member of struct uninitializable [duplicate]

I'm writing some code where I need to have a class variable that's a static int array. I understand that I can do this with something like this in the header file, A.h:
#ifndef A_H_
#define A_H_
class A
{
public:
static const int a[];
};
const int A::a[] = {1,2};
#endif
This works just fine if I'm then including this header in only one other file, something like the following, main.cpp:
#include "A.h"
#include <iostream>
using namespace std;
int main()
{
A myA;
cout << "0: " << myA.a[0] << endl;
cout << "1: " << myA.a[1] << endl;
}
But suppose I need my class A to be a bit more complicated, and I want to have an A.cpp file as well. I'll keep my main.cpp file the same, but then change A.h as follows (I've just added a function, printA):
#ifndef A_H_
#define A_H_
class A
{
public:
void printA() const;
static const int a[];
};
const int A::a[] = {1,2};
#endif
And then in file A.cpp:
#include "A.h"
#include <iostream>
using namespace std;
void A::printA() const
{
cout << "Printing in A.cpp." << endl;
cout << "A.0: " << a[0] << endl;
cout << "A.1: " << a[1] << endl;
}
Compiling A.o with gcc -o A.o -c A.cpp is fine. But linking this when compiling main.cpp (gcc -o atest main.cpp A.o) fails with "multiple definition of `A::a'".
I've been scouring the internet for solutions, and found people who have variables declared in their headers who get the "multiple definition" error when they include the header in multiple places, and the solution seems to be to declare the variable extern in the header, then define it in only one source (non-header) source file. But I can't declare a class variable both static and extern, can I? If I try, or if I just declare it extern, I get a warning about the variable not being static (also a "conflicting specifiers" error when I try both).
So, my question: is it possible to have static array class variables in the case that the header file needs to be included in more than one source file? If so, how?
You're violating the one definition rule. Move the definition inside an implementation file:
//A.cpp
#include "A.h"
const int A::a[] = {1,2};
The solution you are reffering to, with extern, applies to non-member variables. In your case a is a class member.
You should remove the "const int A::a[] = {1,2};" line from the header file. Put this definition line in one of you .cpp files.
Then you can include the header file several times where you need it.

C++ - Initialze static variable with global scope

I have included a static c++ string array in the header file. I get a segfault
when I try to access it in the source file.
Here are the details.
OS : Linux
Compiler : g++
job.hpp
static string values[2] = {"hello","welcome"};
class Job
{
public:
void getValues();
};
job.cpp
#include "job.hpp"
void Job::getValues()
{
// Seg Fault Here
// i value is either 0 or 1 and is based on some external flag
cout << values[i] << endl;
}
I believe the values array is not getting initialized. This code works with xlc++ compiler on AIX. Is there any g++ compiler flag to initialize the static variables.
Why not make your 'values' array into a static attribute of the class? Something like the following might work.
job.h
#ifndef JOB_H
#define JOB_H
#include <string>
#include <iostream>
using namespace std;
class Job
{
public:
Job();
void getValues() const;
//declare static variable in .hpp file
static string values[2];
private:
};
#endif
job.cpp
#include "job.h"
//initialize variable in .cpp file
string Job::values[2]={"hello", "welcome"};
Job::Job(){}
void Job::getValues() const
{
cout << values[i] << endl;
}

C2061: syntax error : identifier 'string' - Behaving weird

I am trying to learn C++, however, the parameter to a method I have in my own class is misbehaving. When it uses a dataType of 'int', it works fine with no errors, but when I attempt to change it to a 'string' dataType, the program crashes with this error.
Error 1 error C2061: syntax error : identifier 'string' in temp.h ln
8 col 1
The classes I am using are as follows:
WORKING CODE
TesterClass.cpp // Entry Point
#include "stdafx.h"
#include "Temp.h"
int _tmain(int argc, _TCHAR* argv[])
{
Temp tmp;
tmp.doSomething(7);
return 0;
}
Temp.h
#pragma once
class Temp
{
public:
Temp();
void doSomething(int blah);
};
Temp.cpp
#include "stdafx.h"
#include "Temp.h"
#include <iostream>
#include <string>
using std::string;
Temp::Temp()
{
std::cout << "Entry" << std::endl;
string hi;
std::cin >> hi;
std::cout << hi << std::endl;
}
void Temp::doSomething(int blah)
{
std::cout << blah;
}
BROKEN CODE
Temp.h
#pragma once
class Temp
{
public:
Temp();
void doSomething(string blah);
};
Temp.cpp
#include "stdafx.h"
#include "Temp.h"
#include <iostream>
#include <string>
using std::string;
Temp::Temp()
{
std::cout << "Entry" << std::endl;
string hi;
std::cin >> hi;
std::cout << hi << std::endl;
}
void Temp::doSomething(string blah)
{
std::cout << blah;
}
When I adjust the parameter 'blah' to be a string, in both the .h and .cpp file, the problem occurs.
I have looked around, but none of the answers seem to solve my problem. I would greatly love help on this an I am out of ideas. I have tried reinstalling C++, messing with:
using namepace std;
using std::string;
std::string instead of string
etc.
If you know how to solve my problem I would love to hear from you. I am more than happy to provide more information.
C++ performs single-pass compilation, so std::string needs to be declared before you use it at all - including in the header file.
// Temp.h
#pragma once
#include <string>
class Temp
{
public:
Temp();
void doSomething(std::string blah);
};
I would encourage you to be specific in your header files when specifying classes like this, because you might easily come across another library that defines it's own string and then you would run into naming conflicts. Save the using import statements for your cpp files.
πάντα ῥεῖ had the write answer, thankyou!
They said to use std::string when needed, and to also #include <string> in the header file.

static array class variable "multiple definition" C++

I'm writing some code where I need to have a class variable that's a static int array. I understand that I can do this with something like this in the header file, A.h:
#ifndef A_H_
#define A_H_
class A
{
public:
static const int a[];
};
const int A::a[] = {1,2};
#endif
This works just fine if I'm then including this header in only one other file, something like the following, main.cpp:
#include "A.h"
#include <iostream>
using namespace std;
int main()
{
A myA;
cout << "0: " << myA.a[0] << endl;
cout << "1: " << myA.a[1] << endl;
}
But suppose I need my class A to be a bit more complicated, and I want to have an A.cpp file as well. I'll keep my main.cpp file the same, but then change A.h as follows (I've just added a function, printA):
#ifndef A_H_
#define A_H_
class A
{
public:
void printA() const;
static const int a[];
};
const int A::a[] = {1,2};
#endif
And then in file A.cpp:
#include "A.h"
#include <iostream>
using namespace std;
void A::printA() const
{
cout << "Printing in A.cpp." << endl;
cout << "A.0: " << a[0] << endl;
cout << "A.1: " << a[1] << endl;
}
Compiling A.o with gcc -o A.o -c A.cpp is fine. But linking this when compiling main.cpp (gcc -o atest main.cpp A.o) fails with "multiple definition of `A::a'".
I've been scouring the internet for solutions, and found people who have variables declared in their headers who get the "multiple definition" error when they include the header in multiple places, and the solution seems to be to declare the variable extern in the header, then define it in only one source (non-header) source file. But I can't declare a class variable both static and extern, can I? If I try, or if I just declare it extern, I get a warning about the variable not being static (also a "conflicting specifiers" error when I try both).
So, my question: is it possible to have static array class variables in the case that the header file needs to be included in more than one source file? If so, how?
You're violating the one definition rule. Move the definition inside an implementation file:
//A.cpp
#include "A.h"
const int A::a[] = {1,2};
The solution you are reffering to, with extern, applies to non-member variables. In your case a is a class member.
You should remove the "const int A::a[] = {1,2};" line from the header file. Put this definition line in one of you .cpp files.
Then you can include the header file several times where you need it.