Get the following errors when I compile with G++ - c++

I am a bit new to programming as you can probably tell from my prior question(s). I was wondering if anyone could help me with this recent problem I've had. I am trying to compile a script main.cpp using g++ but I get the following errors:
Donny#Donny-PC /cygdrive/c/Users/Donny/Desktop/equation/equations/equations
$ g++ main.cpp -o don.exe
main.cpp:3:11: error: ‘::main’ must return ‘int’
void main(){
^
main.cpp: In function ‘int main()’:
main.cpp:36:22: error: ‘pow’ was not declared in this scope
float n=pow(10.0,9.0);
^
main.cpp:43:27: error: ‘sin’ was not declared in this scope
float R56=(lb1/sin(theta1)) * ((tan(theta1))-theta1) + (lb2/sin(theta1)) * ((tan(theta1))-theta1) +
^
main.cpp:43:44: error: ‘tan’ was not declared in this scope
float R56=(lb1/sin(theta1)) * ((tan(theta1))-theta1) + (lb2/sin(theta1)) * ((tan(theta1))-theta1) +
^
main.cpp:48:40: error: ‘cos’ was not declared in this scope
d*((pow(tan(theta1),2))/cos(theta1)) +
^
The weird thing is that this code works when compiled with microsoft visual studio 2010 C++. Any help would be greatly appreciated!
EDIT:
So, the fixed a lot of the errors shown above, but I am still having a little difficulty fixing the void main error. Here is how my code looks:
#include<iostream>
#include<cmath>
using namespace std;
void main(){
float r, i, f, beta, alpha;
cout<<"Enter value of R : ";............
Any help or examples would be greatly appreciated.

The first error should be self-explanatory. The standard says that the main function must return int but you have declared it as void. Return 0 from your main function to indicate normal termination. The Microsoft compiler is not as strict on this point.
All your remaining errors can be remedied by using #include <math.h>.

Related

Undefined reference error for one specific library function

I'm writing software to control a bladeRF radio card but I'm running into a strange compiler/linker error that I haven't been able to figure out. My code uses several functions and data structures defined in the library, libbladeRF, but for some reason I can't reference to one specific function.
However, if I modify the call with an improper argument type, g++ will throw an error to let me know that it doesn't conform to the definition, which seems to tell me that the linker is actually able to locate the reference.
What am I missing?
Initial error:
$ g++ bladeRF_test.cpp -o bladeRF_test -lbladeRF
/tmp/ccTWZzdJ.o: In function `enable_xb300()':
bladeRF_test.cpp:(.text+0x36a): undefined reference to `bladerf_xb300_set_amplifier_enable'
Code excerpt:
#include <iostream>
#include <string>
#include <libbladeRF.h>
using namespace std;
...
int set_xb300_pa(bool enable) {
bladerf_xb300_amplifier amp = BLADERF_XB300_AMP_PA;
if ( bladerf_xb300_set_amplifier_enable(dev, amp, enable) ) {
// Print error message
return -1;
} else {
// Print success message
return 0;
}
}
...
Function arguments changed from (dev, amp, enable) to (&dev, amp, enable):
$ g++ blade_hello.cpp -o blade_hello -lbladeRF
blade_hello.cpp: In function ‘int set_xb300_pa()’:
blade_hello.cpp:62:59: error: cannot convert ‘bladerf**’ to ‘bladerf*’ for argument ‘1’ to ‘int bladerf_xb300_set_amplifier_enable(bladerf*, bladerf_xb300_amplifier, bool)
^
In file included from blade_hello.cpp:4:0:
/usr/local/include/libbladeRF.h:2226:15: note: declared here
int CALL_CONV bladerf_xb300_set_amplifier_enable(struct bladerf *dev,
^

Code compiles in ideone but not with gcc

I wrote the following code:
#include <iostream>
using namespace std;
int main()
{
int v()
return 0;
}
I ran it in ideone, and it compiled successfully. I have the same code in file test1.cpp on my computer, I ran g++ test1.cpp and I got the following error:
./test1.cpp: In function ‘int main()’:
./test1.cpp:7:2: error: a function-definition is not allowed here before ‘return’
Why dose this happen? is this a bug?
I'm using linux mint, gcc version 4.7.
You are missing a semi-colon here:
int v()
^
should be:
int v() ;
which is a function declaration, not clear that was what was intended though. If you want to initialize v then the following would work:
int v(0) ;
or in C++11:
int v{0} ;
This is commonly known as C++'s most vexing parse. When you do something like
int f();
the compiler reads this as a function prototype, declaring a function f that returns an int. If you're using C++11, you should instead do
int f{}; // f initialized to 0
if you're not using C++11, make sure to initialize the variable right away.
You forgot the semicolon after
int v();
Ideone is using gcc 4.8.1 for your code (as you can see in your own link) while you are using 4.7
There are several difference regarding C++ 11 implementation, and apparently it is affected by the line that looks like a function delcaration.

Understanding 'using' keyword : C++

Can someone please explain below output:
#include <iostream>
using namespace std;
namespace A{
int x=1;
int z=2;
}
namespace B{
int y=3;
int z=4;
}
void doSomethingWith(int i) throw()
{
cout << i ;
}
void sample() throw()
{
using namespace A;
using namespace B;
doSomethingWith(x);
doSomethingWith(y);
doSomethingWith(z);
}
int main ()
{
sample();
return 0;
}
Output:
$ g++ -Wall TestCPP.cpp -o TestCPP
TestCPP.cpp: In function `void sample()':
TestCPP.cpp:26: error: `z' undeclared (first use this function)
TestCPP.cpp:26: error: (Each undeclared identifier is reported only once for each function it appears in.)
I have another error:
error: reference to 'z' is ambiguous
Which is pretty clear for me: z exists in both namespaces, and compiler don't know, which one should be used. Do you know? Resolve it by specifying namespace, for example:
doSomethingWith(A::z);
using keyword is used to
shortcut the names so you do not need to type things like std::cout
to typedef with templates(c++11), i.e. template<typename T> using VT = std::vector<T>;
In your situation, namespace is used to prevent name pollution, which means two functions/variables accidently shared the same name. If you use the two using together, this will led to ambiguous z. My g++ 4.8.1 reported the error:
abc.cpp: In function ‘void sample()’:
abc.cpp:26:21: error: reference to ‘z’ is ambiguous
doSomethingWith(z);
^
abc.cpp:12:5: note: candidates are: int B::z
int z=4;
^
abc.cpp:7:5: note: int A::z
int z=2;
^
which is expected. I am unsure which gnu compiler you are using, but this is an predictable error.
You get a suboptimal message. A better implementation would still flag error, but say 'z is ambiguous' as that is the problem rather than 'undeclared'.
At the point name z hits multiple things: A::z and B::z, and the rule is that the implementation must not just pick one of them. You must use qualification to resolve the issue.

Simple C++ program running errors in Xcode, Code Blocks, and Terminal

I'm trying to write a simple program to calculate numerical approximations with Euler's method and every complier I've used hasn't printed anything. Codeblocks is running an error but I think that is because the compiler isn't set up right. xCode will build it but nothing happens. When I run g++ Euler.cpp I get:
Euler.cpp:1: error: expected constructor, destructor, or type conversion before ‘<’ token
Euler.cpp: In function ‘int main()’:
Euler.cpp:13: error: ‘cin’ was not declared in this scope
Euler.cpp:19: error: ‘cout’ was not declared in this scope
Euler.cpp:19: error: ‘endl’ was not declared in this scope
I usually never have problems with simple c++ programs and fear it's something very obvious.
//
// Euler.cpp
// Numerical Approximations (Euler's Method)
//
// Created by XXXXXXXXXXXX on 6/18/12.
// Copyright (c) 2012 University of Kansas Department of Mathematics. All rights reserved.
//
#include <iostream>
using namespace std;
int main ()
{
int N=4;
//cout<<"Number of steps (N):";
//cin>>t;
float h=0.1;
//cout<<endl<<" Step size (h):";
cin>>h;
float y0=1;
//cout<<endl<<"y(0)=";
//cin>>y0;
cout<<"test!"<<endl;
float data[N][4];
int n=0;
data[0][2] = y0;
while (n<N){
data[n][0]=n;
if(n>0){
data[n][2]=data[n-1][3];
}
data[n][1]=h*n;
data[n][3] = data[n][2] + ((3 + data[n][1] - data[n][2])*h);
n++;
cout<<"n="<<n<<". tn="<<data[n][1]<<". y(n)="<<data[n][2]<<". y(n+1)="<<data[n][3] <<"."<<endl;
}
return 0;
}
It's probably something obvious but I don't see it.
It's not finding the iostream header. Do you see an error message reading something like "couldn't find header iostream" ?

'class X' has no member 'Y'

This error is inexplicably occurring. Here is the code and output:
timer.cpp:
#include "timer.h"
#include "SDL.h"
#include "SDL_timer.h"
void cTimer::recordCurrentTime()
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
timer.h:
#include "SDL.h"
#include "SDL_timer.h"
class cTimer
{
private:
int previous_t;
int current_t;
float delta_time;
float accumulated_time;
int frame_counter;
public:
void recordCurrentTime();
float getDelta();
void incrementAccumulator();
void decrementAccumulator();
bool isAccumulatorReady();
void incrementFrameCounter();
void resetFrameCounter();
int getFPS();
};
Compiler errors:
make
g++ -Wall -I/usr/local/include/SDL -c timer.cpp
timer.cpp: In member function ‘void cTimer::recordCurrentTime()’:
timer.cpp:6: error: ‘class cTimer’ has no member named ‘previous_t’
timer.cpp:6: error: ‘class cTimer’ has no member named ‘current_t’
timer.cpp:7: error: ‘class cTimer’ has no member named ‘current_t’
make: *** [timer.o] Error 1
Compiler errors after removing the #include "timer.h"
g++ -Wall -I/usr/local/include/SDL -c ctimer.cpp
ctimer.cpp:4: error: ‘cTimer’ has not been declared
ctimer.cpp: In function ‘void recordCurrentTime()’:
ctimer.cpp:5: error: invalid use of ‘this’ in non-member function
ctimer.cpp:5: error: invalid use of ‘this’ in non-member function
ctimer.cpp:6: error: invalid use of ‘this’ in non-member function
make: *** [ctimer.o] Error 1
Works for me. Are you sure you've got the right timer.h? Try this:
cat timer.h
and verify that it's what you think it is. If so, try adding ^__^ at the beginning of your .h file and seeing if you get a syntax error. It should look something like this:
[/tmp]> g++ -Wall -I/tmp/foo -c timer.cpp
In file included from timer.cpp:1:
timer.h:1: error: expected unqualified-id before ‘^’ token
This seems very odd as
class cTimer
{
private:
int previous_t;
int current_t;
float delta_time;
float accumulated_time;
int frame_counter;
public:
void recordCurrentTime();
float getDelta();
void incrementAccumulator();
void decrementAccumulator();
bool isAccumulatorReady();
void incrementFrameCounter();
void resetFrameCounter();
int getFPS();
};
void cTimer::recordCurrentTime()
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
Compiles OK for me.
This suggests that the compiler think cTimer is different from what you've put in your header. So maybe its getting a definition of cTimer from another source file? For this to be the case your "timer.h" would have to not be gettting included correctly. So maybe the wrong timer.h.
A way to check this would be to save the compiler preprocessor output and search that for cTimer.
Another option might be to put a syntax error in your timer.h and make sure the compile fails.
Anyway hope this helps
Some compilers have their own timer.h, this is a name conflict.
Or it is a something else of bizarre bug...
Try renaming timer.h and timer.cpp to something more descriptive like ClassTimer.h and ClassTimer.cpp, maybe the compiler is linking another file named 'timer' since it is a very generic name. Also try this in timer.cpp:
void cTimer::recordCurrentTime(void)
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
Edit: code edited