I am using geany (code::blocks wouldnt run my programs) as a compiler to compile a simple c++ program with one class. I am on Linux Mint 17 on a Dell Vostro 1500. Compiling works fine with both .cpp files, but the header file gives this error:
gcc -Wall "Morgan.h" (in directory: /home/luke/Documents/Coding/Intro#2)
Morgan.h:5:1: error: unknown type name ‘class’
class Morgan
^
Morgan.h:6:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
Compilation failed.
This is the main.cpp :
#include <iostream>
#include "Morgan.h"
using namespace std;
int main()
{
Morgan morgObject;
morgObject.sayStuff();
return 0;
}
This is the Header file (Morgan.h):
#ifndef MORGAN_H
#define MORGAN_H
class Morgan
{
public:
Morgan();
void sayStuff();
protected:
private:
};
#endif // MORGAN_H
And this is the class (Morgan.cpp):
#include <iostream>
#include "Morgan.h"
using namespace std;
Morgan::Morgan()
{
}
void Morgan::sayStuff(){
cout << "Blah Blah Blah" << endl;
}
I really do not know what is going wrong, so any help would be appreciated. I copy and pasted the same code into a windows compiler and it worked fine, so it might just be the linux.
also when I run the main.cpp this is what shows:
"./geany_run_script.sh: 5: ./geany_run_script.sh: ./main: not found"
You don't compile .h files. Try g++ -Wall main.cpp Morgan.cpp
Your issue is that you are compiling C++ code with a C compiler (GCC). The command you are looking for is g++. The complete command that would compile your code is:
g++ -Wall -o run.me main.cpp Morgan.cpp
If a file is included (In your case the Morgan.h file, you do not need to explicitly compile it. )
Related
I am using VS Code to code C++. It performs totally fine. But whenever I use auto keyword in my code, the program simply fails to Compile.
For example, to iterate over a string my code without using auto keyword will look like
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("Hello");
for(int i=0;i<s.length();i++)
{
cout<<s.at(i)<<' ';
}
cin.get();
}
It compiles find and runs giving Correct Output.
Executing task: g++ -g -o helloworld helloworld.cpp
Terminal will be reused by tasks, press any key to close it.
OUTPUT : H e l l o
But whenever I try to perform the same job but using auto keyword, the code looks like
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("Hello");
for(auto c:s)
{
cout<<c<<' ';
}
cin.get();
}
But it gives Compile Time Error
Executing task: g++ -g -o helloworld helloworld.cpp
helloworld.cpp: In function 'int main()':
helloworld.cpp:7:14: error: 'c' does not name a type
for(auto c:s)
^
helloworld.cpp:11:5: error: expected ';' before 'cin'
cin.get();
^
helloworld.cpp:12:1: error: expected primary-expression before '}' token
}
^
helloworld.cpp:12:1: error: expected ')' before '}' token
helloworld.cpp:12:1: error: expected primary-expression before '}' token
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.
Please help me out.
This is the clue:
Executing task: g++ -g -o helloworld helloworld.cpp
I suspect you need to compile with -std=c++11 or later.
Older builds of gcc/g++ will default to C++ 98 standard before the auto keyword was introduced. There might be other configurations where this is the default as well. The workaround is simple.
Configure your build such that the task being compiled is this:
g++ -std=c++11 -g -o helloworld helloworld.cpp
You can also use -std=c++14 or -std=c++17 if available.
I know there are many similar topics but there are equally many unique mistakes that may lead to this problem (so I think). Therefore I ask, after some research.
My problem is that the compiler, GNU GCC, when compiling one file does not see my namespace declared in another file. The IDE (CodeBlocks) evidently does see it as it auto-completes the name of the namespace. I tried to isolate the problem and came up with this:
File main.cpp:
namespace MyName
{
int MyVar;
}
#include "T1.cpp"
int main()
{
return 0;
}
File T1.cpp:
using namespace MyName;
error: 'MyName' is not a name-space name.
In my project I have a header file, say T1.h, and an implementation file T1.cpp — and MyName isn't accessible in either of them.
Any help or guidance would be appreciated.
What's happening is that CodeBlocks is compiling both main.cpp and T1.cpp. Here is what happens when you try to compile each one:
main.cpp:
$ g++ main.cpp
$
T1.cpp
$ g++ T1.cpp
T1.cpp:1:17: error: ‘MyName’ is not a namespace-name
using namespace MyName;
^
T1.cpp:1:23: error: expected namespace-name before ‘;’ token
using namespace MyName;
^
$
T1.cpp, when compiled on it's own, has no knowledge of MyName. To fix this, don't include .cpp files, and put your declarations in header files.
Edit: From what I gather, this may be a better way to organize your example:
T1.h:
namespace MyName {
extern int MyVar;
}
T1.cpp
#include "T1.h"
int MyName::MyVar = 5;
main.cpp
#include "T1.h"
#include <iostream>
using namespace MyName;
int main()
{
std::cout << MyVar << std::endl;
return 0;
}
Now it will compile correctly:
$ g++ -c T1.cpp -o T1.o
$ g++ -c main.cpp -o main.o
$ g++ T1.o main.o
$ ./a.out
5
I'm using the gcc compiler on Windows with MinGW. Version is 4.9.3.
The following code gives errors when -std=c++98, -std=c++03 or -std=c++11 is used as an argument.
#include <iostream>
int main()
{
std::cout << "Hello world!" << std::endl;
return 0;
}
The code compiles with no errors when -std=gnu++98, -std=gnu++03 or std=gnu++11 is used as an argument. Also, the code compiles with no errors when using no c++ version arguments (g++ test.cpp -c)
On further investigation I found it was the #include causing issues.
This code produces no errors when using any of the std=c++ arguments:
int main()
{
return 0;
}
However, when looking for other things to include to test my code, the following works:
#include <cmath>
int main()
{
return 0;
}
but this doesn't:
#include <string>
int main()
{
return 0;
}
What's going on? From a brief search on gnu++, it says that it provides additional extensions but code as simple as the ones above surely shouldn't be reliant on any extensions?
I've pasted the large error which occurs when compiling the first piece of code with g++ test.cpp -c -std=c++11. http://pastebin.com/k0RLtWQz
The first messages are:
$ g++ test.cpp -c -std=c++11
In file included from c:\mingw\include\wchar.h:208:0,
from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\cwchar:44,
from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\bits\postypes.h:40,
from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\iosfwd:40,
from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\ios:38,
from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\ostream:38,
from c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\iostream:39,
from test.cpp:1:
c:\mingw\include\sys/stat.h:173:14: error: '_dev_t' does not name a type
struct _stat __struct_stat_defined( _off_t, time_t );
^
c:\mingw\include\sys/stat.h:173:14: error: '_ino_t' does not name a type
struct _stat __struct_stat_defined( _off_t, time_t );
^
…
Solved by changing to mingw64 (which also uses a newer version of gcc). Seems like the problem was with either my mingw32 installation or with the distribution (as pointed out by Jonathan Leffler).
All the -std=c++xx parameters now work.
On one of my Mac box, for simple Hello Word C++ program, there are such compile error, other Macs I am working on are ok. Using even the same version of Eclipse CDT 64-bit Mars.
Posted error and Hello Word program, does anyone have any hints? Thanks.
//============================================================================
// Name : Test1.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
Attach error from g++ of command line,
g++ Test1.cpp
Test1.cpp:9:20: error: iostream: No such file or directory
Test1.cpp: In function ‘int main()’:
Test1.cpp:13: error: ‘cout’ was not declared in this scope
Test1.cpp:13: error: ‘endl’ was not declared in this scope
In summary, if whether you're developing in C, C++ or Objective C on the Mac, you probably just want to use XCode.
Not sure what Eclipse was doing, but (see link in comments) earlier versions of Eclipse might work where later ones will not.
Alright im compiling the following simple chunk of code (found on cplusplus.com) on CodeBlocks IDE 12.11 with MinGW (downloaded separately and latest version too as of today).
The thing is that it shows the following errors upon compilation:
12: error: 'thread' was not declared in this scope
12: error: expected ';' before 't1'
13: error: 't1' was not declared in this scope
#include <iostream>
#include <thread>
using namespace std;
void hello(void){
cout << "hey there!" << endl;
}
int main()
{
thread t1(hello);
t1.join();
return 0;
}
Are threads not supported by GCC completely?Do i need to add flags to my compiler, and how do i do it on a codeblocks project? thanks in advance
Add --std=c++11 -pthread to your Compiler Flags