I have installed CLN from cygwin and it has an examples folder. I copied an example and tried to compile it and it wont let me
#include <cln/real.h>
#include <cln/integer.h>
#include <cln/io.h>
#include <cln/integer_io.h>
#include <cln/numtheory.h>
using namespace std;
using namespace cln;
int main() {
int n;
cin >> n;
cl_R x = (cl_R) n;
cl_I p = nextprobprime(x);
cout << p << "\n";
return 0;
}
It gives an error
fatal error: cln/real.h: No such file or directory
1 | #include <cln/real.h>
| ^~~~~~~~~~~~
compilation terminated.
My plan was to install ginac so I had to install cln first. Do I need to add a path variable or something to allow it to compile? If so how do I do that
Also is it possible to write
#include <cln>
or something like this so that all headers are included instead of doing it one by one. This is my first external library. I plan to use more
Related
When i try to run a code with the function "strtok" in it i get the error code 0x80070002. I included cstring, cctype, string.h and i also tried using /DEBUG FULL in Properties - Linker - Debugging, like a few other posts said but it still doesn't work. Any clues why VS doesn't work with strtok? I also tried reinstalling VS and running a simple code like this:
#include <iostream>
#include <cstring>
#include <cctype>
#include <string.h>
using namespace std;
int main()
{
char s[100], * p;
cin.getline(s, 100);
p = strtok(s, " ");
cout << p;
return 0;
}
The desired behaviour would be to show me the first word of s. Even when i try to run the code at https://en.cppreference.com/w/cpp/string/byte/strtok i get the same error.
There are two methods to meet your needs;
Add _CRT_SECURE_NO_WARNINGS in Properties->C/C++->Preprocessor->Preprocessor Definitions.
Use strtok_s instead of strtok:
int main()
{
char *buf;
char s[100], *p;
cin.getline(s, 100);
p = strtok_s(s, " ", &buf);
cout << p;
return 0;
}
So let's say i have this code that looks for documents of mine and prints the path to them.
#include <iostream>
#include <experimental/filesystem>
#include <string>
#include <stdio.h>
#include <Shlwapi.h>
using namespace std;
string extensions[3] = { ".doc", ".docx", ".txt" }; // things to look for
string ignoredirs[2] = { "Windows", "Program Files", } // and other ones that i was too
// lazy to write in there
using namespace std::experimental::filesystem;
path yee;
void main()
{
for (recursive_directory_iterator i("c:\\"), end; i != end; ++i)
if (!is_directory(i->path()) && i->path().has_extension()) // checks if the file
// even has an extension
{
for (int x = 0; x <= 3; ++x)
if (i->path().extension().string() == extensions[x])// checks if the
// extension is equal
// to current
// extension in loop
cout << "found document at :" ;
cout << i->path().string() << endl; // print out the path
}
}
And I would like to not iterate to directories in ignoredir[] because it takes ages to find my docs on the filesystem.
I saw this code from cppreference.com.
Could someone explain me the the code and/or how to use it in my use case?
Pr could you submit a better solution than that?
Ps. I don't want to use boost in this program, just to see how it works in experimental::filesystem
Proposed solution:
#include <iostream>
#include <experimental/filesystem>
#include <string>
#include <stdio.h>
#include <Shlwapi.h>
#include <set>
using namespace std;
set<string> extensions = { ".doc", ".docx", ".txt" }; //things to look for
set<string> ignoredirs = { "Windows", "Program Files" }; //and other ones that i was too lazy to write in there
using namespace std::experimental::filesystem;
int main()
{
for (recursive_directory_iterator i("c:\\"), end; i != end; ++i)
{
if (!is_directory(i->path()) && i->path().has_extension()) // checks ifthe file even has an extension
{
if (extensions.find(i->path().extension().string()) != extensions.end())
cout << "found document at :";
cout << i->path().string() << endl; //print out the path
}
if (ignoredirs.find(i->path().filename().string()) != ignoredirs.end())
i.disable_recursion_pending();
}
}
Explanation:
i->path().filename() actually return directory name, when the same directory name is in set<> then i.disable_recursion_pending(); is called. When this one is called recursive_directory_iterator i omit directory with i->path().filename() name.
set<string> was used to get rid internal for loop that requires size of table, which is error prone. Also performance gain and code simplification
Notice:
On windows directory names should be compared case insensitive. Also Windows 64bit have "Program Files" and "Program Files (x86)" and both need to be listed in ignore list (or string comparison need to be improved)
I'm learning C++ and tutorial asks me to add another project to what I have now.
Also I'm asked to use forward declaration so I can make use of that added file.
Here is my main project:
#include <iostream>
#include "io.cpp"
using namespace std;
int readNumber();
void writeResult(int x);
int main() {
int x = readNumber();
int y = readNumber();
writeResult(x + y);
return 0;
}
here's the added file called io.cpp:
#include <iostream>
using namespace std;
int readNumber() {
cout << "Enter a number: ";
int x;
cin >> x;
return x;
}
void writeResult(int x) {
cout << "Sum of your numbers is " << x << endl;
}
![And here's a screenshot so you can see what error I'm getting which talks about multiple definition and you can see where those two files are added.
According to the tutorial my code is okay but compiler complains. Why ?]1
In codeblocks, when creating a new class, it should automatically header file. Programming with header files is the best practice out there. Here's the code I tried and it worked, with io.h.
main.cpp
#include <iostream>
#include "io.h"
using namespace std;
io inOut;
int main()
{
int x = inOut.readNumber();
int y = inOut.readNumber();
inOut.writeResult(x + y);
return 0;
}
io.h
#ifndef IO_H
#define IO_H
class io
{
public:
int readNumber();
void writeResult(int);
};
#endif
io.cpp
#include <iostream>
#include "io.h"
using namespace std;
int io::readNumber()
{
cout << "Enter a number: ";
int x;
cin >> x;
return x;
}
void io::writeResult(int x)
{
cout << "Sum of your numbers is " << x << endl;
}
I used codeblocks to compile the code written above, and it worked perfectly.
Well as turns out when adding more cpps they're not supposed to be #included on the top. That's what makes compiler say that function is being defined multiple times. All I had to do was just get rid off that one line.
Here's my source:
http://www.cplusplus.com/forum/beginner/44651/
I have built the facebook folly libraries by using the following link given below
https://github.com/facebook/folly
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <typeinfo>
#include <vector>
#include "folly/FBVector.h"
int main()
{
folly::fbvector<int> vec;
folly::fbvector<int>::iterator vIter;
int i = 0;
for(i = 0;i < 10;i++)
{
vec.push_back(i);
}
for(i = 0 ; i <=vec.size();i++)
{
cout << vec[i] << endl;
}
vec.erase(vec.begin()+ 5);
vec.insert(vec.begin()+5,25);
vec.insert(vec.begin()+5,5);
vec.insert(vec.begin()+5,5);
sort(vec.begin(),vec.end());
cout << "Using Iteration Concept " << endl;
for(vIter = vec.begin() ; vIter != vec.end();vIter++)
{
cout << *vIter << endl;
}
return 0;
}
After writting this code ,just compiled it..when i was compiling,getting the following issue..
syscon#syscon-OptiPlex-3020:~/Documents/work/folly/folly-master$ g++ -o stl stl.cpp -std=c++11 -lboost_system
/tmp/cc2WeDlr.o: In function folly::usingJEMalloc()':stl.cpp:(.text._ZN5folly13usingJEMallocEv[_ZN5folly13usingJEMallocEv]+0x2d): undefined reference tofolly::usingJEMallocSlow()'
collect2: error: ld returned 1 exit status
which library i need to include inorder to solve this issue?
That particular symbol comes from: https://github.com/facebook/folly/blob/master/folly/Malloc.cpp so it's part of the compiled folly library (ie, it's not just a header-only dependency). You need to link against it.
Depending on how you compiled/installed folly will determine how to fix the problem, e.g. adding -lfolly to the compilation line.
I am trying to cout some variables but the compiler says that cout is undefined. I have included iostream and am using namespace std. Removing using namespace std and using std::cout instead changes the issue to "namespace "std" has no member "cout" ". I found some answers saying to add # include "stdafx.h" to the code but Error: cannot open source file "stdafx.h" occurs.
Code is:
#include "Complex.h"
#include <cmath>
#include <iostream>
using namespace std;
Complex::Complex(int PolarOrRectang, float RealOrArg, float ImagOrAng) {
if (PolarOrRectang == 0) {
real = RealOrArg;
imag = ImagOrAng;
else {
real = RealOrArg * cos(ImagOrAng);
imag = RealOrArg * sin(ImagOrAng);
}
};
void Complex::getValue(int PolarOrRectang) {
if (PolarOrRectang == 0) {
cout << real << " +_" << imag << "i" << endl;
} else {
cout << sqrt((real^2) + (imag^2)) << "*e^-" << atan(imag / real)<< endl;
}
};
I'm trying to define a class, so my main is elsewhere.
Running a very basic program that just couts "hello world" works fine, the problem is specific to this code.
Put #include<iostream> at the first position, the order is important
#include "Complex.h"
#include <iostream>
#include <cmath>
PS: Why do you use std:: when you are using "using namespace std;"?