g++ is unable to find my header file - c++

I'm trying to compile the file q1.cpp but I keep getting the compilation error:
q1.cpp:2:28: fatal error: SavingsAccount.h: No such file or directory
compilation terminated.
The header file and the implementation of the header file are both in the exact same directory as q1.cpp.
The files are as follows:
q1.cpp:
#include <iostream>
#include <SavingsAccount.h>
using namespace std;
int main() {
SavingsAccount s1(2000.00);
SavingsAccount s2(3000.00);
}
SavingsAccount.cpp:
#include <iostream>
#include <SavingsAccount.h>
using namespace std;
//constrauctor
SavingsAccount::SavingsAccount(double initialBalance) {
savingsBalance = initialBalance;
}
SavingsAccount::calculateMonthlyInterest() {
return savingsBalance*annualInterestRate/12
}
SavingsAccount::modifyInterestRate(double new_rate) {
annualInterestRate = new_rate;
}
SavingsAccount.h:
class SavingsAccount {
public:
double annualInterestRate;
SavingsAccount(double);
double calculateMonthlyInterest();
double modifyInterestRate(double);
private:
double savingsBalance;
};
I'd like to reiterate that all files are in the SAME directory. I'm trying to compile by using this line at a windows command prompt:
C:\MinGW\bin\g++ q1.cpp -o q1
Any input into this problem would be appreciated.

#include <SavingsAccount.h>
should be
#include "SavingsAccount.h"
since SavingsAccount.h is the header file you defined, you should not ask the compiler to search for system headers by using <> around it.
Meanwhile, when you compile it, you should compile both cpp files: SavingsAccount.cpp and q1.cpp.
g++ SavingsAccount.cpp q1.cpp -o q1
BTW: you missed a ; here:
SavingsAccount::calculateMonthlyInterest() {
return savingsBalance*annualInterestRate/12;
//^^; cannot miss it
}

Related

C++ Undefined Reference When Compiling .h and .cpp in Subfolders

Long story short I want to put my .h and .cpp files in subfolders (include and src respectively) and reference them in my main.cpp file but I am receiving an error of:
main.cpp:(.text+0x47): undefined reference to `Kmer::Kmer()'.
when compiling using:
g++ -I /path/to/MyFolder/include main.cpp.
My files are structured like below:
MyFolder
main.cpp
include
Kmer.h
src
Kmer.cpp
//main.cpp
#include <iostream>
#include "Kmer.h"
using namespace std;
int main() {
Kmer k;
return 0;
};
//Kmer.h
#pragma once
class Kmer{
public:
Kmer();
protected:
private:
};
//Kmer.cpp
#include "Kmer.h"
#include <iostream>
using namespace std;
Kmer::Kmer(){
// code here
cout << "Kmer created" << endl;
}
I appreciate the help!
You are not compiling Khmer.cpp. You need to add it to your g++ compile line
g++ -o <YOUR APPLICATION NAME> -I /path/to/MyFolder/include main.cpp src/Khmer.cpp

Why are my functions undefined when I declared the type already?

Hi I was just trying to learn separate Classes in C++. I don't know why my code is not working.
So here is the main file code
#include <iostream>
#include "Number.h"
using namespace std;
int main()
{
Number key;
key.setNumber(200);
cout<<key.getNumber();
return 0;
}
Here is the Class cpp functions file code
#include "Number.h"
#include <iostream>
using namespace std;
void Number::setNumber(int transfernumber)
{
privatenumber = transfernumber;
}
int Number::getNumber()
{
return privatenumber;
}
And here is the header file
#ifndef NUMBER_H
#define NUMBER_H
class Number
{
public:
Number();
void setNumber(int transfernumber);
int getNumber();
private:
int privatenumber;
};
#endif // NUMBER_H
Thanks
In your cpp file you need to define the default constructor for the Number class. For example:
Number::Number() : privatenumber(0) {}
I have test your example. The error happened for the main.cpp cannot found the number.cpp. You have three ways to solve it:
write your main() to the number.cpp, not a solo file.
complie the main.cpp with the linux command gcc or write a Makefile, instead of using codeblocks.
If you want to use the codeblocks for compiling, you should create a project, and then add your three files to the project. Now compile the main.cpp.
Use the three ways above, I think you will compile successfully.
BTW, you should add the Number::Number() 's implementation.

Trying to link a test program to a library, getting error: iostream: No such file or directory

I'm trying to create a c++ library for the purpose of controlling a signal generator. I have a working script that compiles into an executable that runs the way I want, and now I want to create a static library with a 'signal' class such that the code can be integrated into a larger library being created by the research collaboration I'm part of for all the hardware we have. However, I'm having trouble compiling a test program (test.cc) for the signal.cpp source code and signal.h that I've written.
Here's signal.cpp:
#include "signal.h"
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
signal::signal(){
myfile.open("/dev/usbtmc1");
}
signal::~signal(){
myfile.close();
}
int signal::on(){
myfile<<"*RST\n";
myfile<<":DISPLAY OFF\n";
myfile<<"FUNC1:PULS:HOLD WIDT\n";
myfile<<"FUNC1:PULS:TRAN:LEAD 2.5NS\n";
myfile<<"FUNC1:PULS:TRAN:TRA 2.5NS\n";
myfile<<":FUNC1:PULS:WIDT 20NS\n";
myfile<<":FUNC1 PULS\n";
myfile<<":VOLT1 5.0V\n";
myfile<<":VOLT1:OFFS 1.797V\n";
myfile<<":FREQ1 100HZ\n";
myfile<<":OUTP1:IMP:EXT MAX\n";
myfile<<":ARM:SOUR1 IMM|INT\n";
myfile<<":OUTP1 ON\n";
myfile<<":OUTP1:COMP ON\n";
return 0;
}
int signal::off(){;
myfile<<"*RST\n";
return 0;
}
int signal::write(char comstring[80]){
strcat(comstring,"\n");
myfile<<comstring;
return 0;
}
Here's signal.h:
#ifndef SIGNAL_H
#define SIGNAL_H
#include <iostream>
#include <fstream>
#include <string.h>
//using namespace std;
class signal{
private:
std::ofstream myfile;
public:
signal();
~signal();
int on();
int off();
int write(char comstring[80]);
};
#endif
And the little test program I've written to try out calling the signal class:
#include "signal.h"
using namespace std;
int main(){
signal ser;
ser.on();
}
I can get the signal.cpp and the signal.h files to compile into a signal.so dynamic object, but when I try to call 'g++ test.cc -o test -l signal.h' at the terminal, I get the error:
signal.h:4:20: error: iostream: No such file or directory
signal.h:5:19: error: fstream: No such file or directory
signal.h:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘signal’
I'm confused by this, as I thought iostream and fstream were part of the c++ standard library and therefore wouldn't need to be linked when compiling using g++. Could anyone please illuminate me as to what I should fix or try to sort this? Many thanks. Sam.
Hoping your all files are in same folder.
g++ -I . test.cpp signal.cpp -o test

Xcode won't detect files included from main in included file

here is the structure of my program:
// File: main.cpp
#include <iostream>
#include <math.h>
using namespace std;
#include "do.cpp"
int main()
{
doit();
}
// File: do.cpp
void doit()
{
cout<<sqrt(2)<<endl;
}
When I do
g++ main.cpp
Everything goes fine. Now, if I open this as an Xcode project (I have chosen "Command line utility" as project type) and try to just build and run, it keeps saying:
Use of undeclared identifier sqrt
Unknown type name 'ostream'
What should I be supposed to do? Did I do something wrong?
Thank you very much!
Matteo
Don't do this:
#include "do.cpp"
but instead put that "do.cpp" file in the same Xcode project, alongside your main.m or main.cpp file.
And when you want to build from the command line, you can do:
g++ main.cpp do.cpp -o mytesttool
which would create the command line tool named "mytesttool".
The explanation is quite simple actually. You probably added both files to the project. Xcode tries to compile each file into an object file and then link them together.
When it tries to compile do.cpp it doesn't find the definition of cout because iostream is not included and neither math.h for sqrt, as part of do.cpp.
That file compiles fine when compiled as part of main.cpp, because it is included in the file and it finds iostream and math.h and also the using declaration.
Anyway if you remove do.cpp from the project (just the reference) everything should compile as expected.
The right way without a header file
// File: main.cpp
void doit(); // declare the function
int main()
{
doit();
}
// File: do.cpp
#include <iostream>
#include <math.h>
using namespace std;
void doit()
{
cout<<sqrt(2)<<endl;
}
The right way with a header file
// File do.h
#ifndef __DO_H_
#define __DO_H_
void doit();
#endif // __DO_H_
// File: main.cpp
#include "do.h"
int main()
{
doit();
}
// File: do.cpp
#include <iostream>
#include <math.h>
#include "do.h"
using namespace std;
void doit()
{
cout<<sqrt(2)<<endl;
}
When making the new file, I forgot to de-check the "target" selection, so that when it tried to build the project it tried to build all the single files and then link them together. By disabling the "target", I got it to work.

Error when trying to separating class into .h, .cpp

This is a minimal program that I made to understand this problem better.
ADT.h
#ifndef ADT_H
#define ADT_H
class ADT {
public:
void print();
};
#endif
ADT.cpp
#include <iostream>
#include "ADT.h"
using namespace std;
void ADT::print()
{
cout << "This program works." << endl;
}
testADT.cpp
#include <iostream>
#include "ADT.h"
using namespace std;
int main(void)
{
ADT sa;
sa.print();
return 0;
}
I compiled it with the vim/minGW compiler my school provided me like so:
g++ testADT.cpp
Which produced the following error:
C:\Users\King\AppData\Local\Tempcc6eoWAP.o:testADT.cpp(.text+0x15 reference to 'ADT::print()'
collect2.exe error: ld returned 1 exit status
Can you explain this error message and indicate the error in my code?
You didn't post the error, but I see that you're missing the semicolon after void print()in the header.
EDIT: That's a linker error. Each source file should be compiled into an object file; then the object files linked:
g++ -c -oADT.o ADT.cpp
g++ -c -otestADT.o testADT.cpp
g++ -oADT ADT.o testADT.o
You can also do it in one line as in michaeltang's answer, but then you can't recompile the sources individually (the 2 step method scales better).
You should also compile ADT.cpp
g++ -o testadt testADT.cpp ADT.cpp