/*
* File: main.cpp
* Author: c1004527
*
* Created on February 15, 2012, 10:25 PM
*/
#include <stdlib.h>
#include <ctime>
#include <time.h>
#include "CardClass.h"
#include "PlayerClass.h"
/*
*
*/
int main(int argc, char** argv)
{
srand(time(0));
CardClass deck;
PlayerClass player[4];
deck.ShuffleCards();
deck.Print();
for(int i = 0; i < DECK_SIZE; i++)
{
player[i%4].AddACard(deck.DealCards());
}
for(int i = 0; i < 4; i++)
{
player[i].SortCard();
}
for(int i = 0; i < 4; i++)
{
cout << endl << endl
<< "Player "<< i+1 << endl
<< player[i];
}
return (0);
}
The error is:
**** Internal Builder is used for build
**** g++ -O0 -g3 -Wall -c -fmessage-length=0 -o OLA3\OLA3\main.o ..\OLA3\OLA3\main.cpp
..\OLA3\OLA3\main.cpp: In function 'int main(int, char**)':
..\OLA3\OLA3\main.cpp:17:17: error: 'time' was not declared in this scope
Build error occurred, build is stopped
Time consumed: 114 ms.
Everything else compiles completely fine but that. I use minGW and it compiles hello world with no problem.
Because you've included <ctime>, time is going to be defined in the std namespace.
Therefore, you need to use a fully-qualified reference: std::time.
That's what this error is telling you:
error: 'time' was not declared in this scope
Related
I'm extracting a tuple with auto [...] but I'm only using some of the tuple coordinates.
I wanted to know if there is some elegant way to avoid the unused-variable compiler warning?
Here is my code.
#include <iostream>
#include <vector>
#include <tuple>
int main(int argc, char **argv)
{
std::vector<std::tuple<char,float,int>> myvec;
myvec.push_back(std::make_tuple('a',3.5,8));
myvec.push_back(std::make_tuple('b',1.5,4));
auto [c,f,_] = myvec[1];
std::cout << "char float is " << c << " " << f << "\n";
return 0;
}
And here is the compilation line + warning:
$ g++ -Wall -std=c++17 main.cpp -o main
main.cpp: In function ‘int main(int, char**)’:
main.cpp:10:13: warning: unused variable ‘_’ [-Wunused-variable]
auto [c,f,_] = myvec[1];
^
(I used a Haskell-like variable _ to self document the fact that the last int is a don't-care value).
I unable to find a solution to my problem, I think it has something to do with overloading functions but I can't seem to figure out how to resolve it.
here is my function.cpp
#include "CountLetter.h"
int Countletter(string sentence, char letter) {
int size = sentence.length();
int toReturn = 0;
for (int i = 0; i<= size; ++i) {
if (sentence[i] == letter) {
toReturn++;
}
}
return toReturn;
}
Here is my function.h
#ifndef FN_H
#define FN_H
#include <iostream>
using namespace std;
int CountLetter(string sentence, char letter);
#endif
My main.cpp
#include "CountLetter.h"
int main() {
string sent = "";
char let = ' ';
int times = 0;
cout << "Enter a sentence.\n";
getline(cin, sent);
cout << "Enter a letter.\n";
cin >> let;
times = CountLetter(sent, let);
cout << "The letter " << let << " occurred " << times << " time(s).\n";
return 0;
}
and finally my makefile
lab16: lab16.o CountLetter.o
g++ -std=c++11 -o lab16 lab16.o CountLetter.o
lab16.o: lab16.cpp
g++ -std=c++11 -o lab16.o -c lab16.cpp
CountLetter.o: CountLetter.h CountLetter.cpp
g++ -std=c++11 -o CountLetter.o -c CountLetter.cpp
and my errors
lab16.o: In function `main':
lab16.cpp:(.text+0xb4): undefined reference to
`CountLetter(std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >, char)'
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'lab16' failed
make: *** [lab16] Error 1
Thanks!
C and C++ are case sensitive:
Definition:
int Countletter(string sentence, char letter) {
Usage:
times = CountLetter(sent, let);
When facing with a linker error, suspect misspelling as one of the scenarios.
Did you get their subtle difference?
Countletter
CountLetter
Your CountLetter function is in the function.h so
change your #include "CountLetter.h" into #include "function.h"
I want to do a fft in my c++ project, and show it afterwards as an image. In order to do the fft, I am using fftw++, and for displaying the image I wanted to use the CImg-library. Thus I started with the demo project from here. When compiling it, everything works. As soon as I add the CImg-header, it fails with the error
test.cpp: In function ‘int main()’:
test.cpp:18:12: error: ‘f’ was not declared in this scope
Complex *f=ComplexAlign(n);
My file looks like
#include "fftw++.h"
#include "CImg.h"
// Compile with:
// g++ -I .. -fopenmp example0.cc ../fftw++.cc -lfftw3 -lfftw3_omp
//using namespace std;
//using namespace utils;
//using namespace fftwpp;
//using namespace cimg_library;
int main()
{
fftwpp::fftw::maxthreads=get_max_threads();
std::cout << "1D complex to complex in-place FFT, not using the Array class"
<< std::endl;
unsigned int n=4;
Complex *f=utils::ComplexAlign(n);
fftwpp::fft1d Forward(n,-1);
fftwpp::fft1d Backward(n,1);
for(unsigned int i=0; i < n; i++) f[i]=i;
std::cout << "\ninput:" << std::endl;
for(unsigned int i=0; i < n; i++) std::cout << f[i] << std::endl;
Forward.fft(f);
std::cout << "\noutput:" << std::endl;
for(unsigned int i=0; i < n; i++) std::cout << f[i] << std::endl;
Backward.fftNormalized(f);
std::cout << "\ntransformed back:" << std::endl;
for(unsigned int i=0; i < n; i++) std::cout << f[i] << std::endl;
utils::deleteAlign(f);
}
and is compiled with
g++ -I .. -fopenmp test.cpp ../fftw++.cc -lfftw3 -lfftw3_omp
My g++ version is 4.8.5. Adding the Complex.h-header does not help either. What can I do in order to combine both libraries?
Edit: Further research shows that using the C-library complex.h and CImg.h results in a lot of compilation problems, combining the library Complex.h from the fftw++-package results also in errors, only the complex-include from C++ can be used together with the CImg.h-include file. Reason: Unknown till now.
My solution (even if it is not perfect) is, that I created a second cpp-file:
second.cpp:
#include "CImg.h"
//Code for images
void example(void){
}
and an include-file for that:
second.h:
#ifndef SECOND_H
#define SECOND_H
void example(void);
#endif /* SECOND_H */
If I only include that include file instead of CImg.h, I can use both fftw++ and CImg.
This program allows the user to enter expenses for each season and then displays the values and then the total cost at the bottom. It's giving me this error, however,
In file included from /usr/include/c++/4.8.3/array:35:0,
from main.cpp:2:
/usr/include/c++/4.8.3/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support for the \
How do I fix this?
#include <iostream>
#include <array>
#include <string>
// constant data
const int Seasons = 4;
const std::array<std::string, Seasons> Snames =
{"Spring", "Summer", "Fall", "Winter"};
// function to modify array object
void fill(std::array<double, Seasons> * pa);
// function that uses array object without modifying it
void show(std::array<double, Seasons> da);
int main()
{
std::array<double, Seasons> expenses;
fill(&expenses);
show(expenses);
return 0;
}
void fill(std::array<double, Seasons> * pa)
{
using namespace std;
for (int i = 0; i < Seasons; i++)
{
cout << "Enter " << Snames[i] << " expenses: ";
cin >> (*pa)[i];
}
}
void show(std::array<double, Seasons> da)
{
using namespace std;
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < Seasons; i++)
{
cout << Snames[i] << ": $" << da[i] << endl;
total += da[i];
}
cout << "Total Expenses: $" << total << endl;
}
Read the error message! You need to compile with -std=c++11 or -std=gnu++11.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Float to binary in C++
I want to print out the binary representation of a float number in C++. Not very practical, just out of curiosity.
The following program doesn't compile though. The reinterpret_cast fails. What kind of cast can I use so that I can do the " &(1 << i) " part?
#include <iostream>
using namespace std;
void toBinary(float num) {
int numi = reinterpret_cast<int>(num);
cout << num << " " << numi << endl;
for (int i = 0; i < 8 * sizeof(num); i++){
if (numi & (1<<i)) {
cout << 1;
} else {
cout << 0;
}
}
cout << endl << endl;
}
int main() {
float a;
cout << sizeof(int) << " " << sizeof(float) << endl;
a = 13.5;
toBinary(a);
toBinary(13.9);
toBinary(2 * a);
toBinary(-a);
}
There's a much easier way. Take a pointer to the float, and reinterpret_cast it to a pointer to char. Now loop through sizeof(float) and convert each char to 8 binary digits. This method works for doubles too.
Use a union. I did this code to do exactly what you want:
// file floattobinary.cc
#include <string>
#include <inttypes.h> // for uint32_t
using namespace std;
void floatToBinary(float f, string& str)
{
union { float f; uint32_t i; } u;
u.f = f;
str.clear();
for (int i = 0; i < 32; i++)
{
if (u.i % 2) str.push_back('1');
else str.push_back('0');
u.i >>= 1;
}
// Reverse the string since now it's backwards
string temp(str.rbegin(), str.rend());
str = temp;
}
Below is a test program to run this function:
// file test.cc
#include <iostream>
#include <string>
#include <cstdlib> // for atof(3)
using namespace std;
void floatToBinary(float, string&);
int main(int argc, const char* argv[])
{
string str;
float f;
if (argc > 1)
{
f = static_cast<float>(atof(argv[1]));
floatToBinary(f, str);
}
cout << str << endl;
return 0;
}
Compile and run (I'm using GNU g++ on Linux):
me#mypc:~/college/c++/utils$ g++ -c floattobinary.cc
me#mypc:~/college/c++/utils$ g++ -c test.cc
me#mypc:~/college/c++/utils$ g++ -o test *.o
me#mypc:~/college/c++/utils$ ls
floattobinary.cc floattobinary.o test* test.cc test.o
me#mypc:~/college/c++/utils$ ./test 37.73
01000010000101101110101110000101
me#mypc:~/college/c++/utils$ ./test 2.0
01000000000000000000000000000000
me#mypc:~/college/c++/utils$ ./test 0.0
00000000000000000000000000000000
me#mypc:~/college/c++/utils$ ./test 237.74
01000011011011011011110101110001
me#mypc:~/college/c++/utils$ ./test 2.74e12
01010100000111110111110100101111
me#mypc:~/college/c++/utils$ ./test 2.74e13
01010101110001110101110001111010
me#mypc:~/college/c++/utils$ ./test -88.37
11000010101100001011110101110001