Ok, I'm having a weird problem with getting my code to compile. The compiler (g++) keeps telling me that the "copy constructor" is private and gives me "error: within this context". Here's my following code:
Code:
#include <iostream>
#include <cmath>
class Crypt {
private:
std::istream strm;
int n;
int p;
int k; // want to find this value
public:
// problem is here
Crypt(std::istream &is): strm(is), n(0), p(0), k(0)
{
}
void getpair();
void print()
{
std::cout << "n: " << n << " p: " << p << " k: " << k << std::endl;
}
int calculate();
};
int main(int argc, char **argv)
{
Crypt cval(std::cin); /* apply default constructor */
cval.getpair();
std::cout << cval.calculate() << std::endl;
return 0;
}
inline void Crypt::getpair()
{
/* could use some improvement by checking */
int val1;
std::cin >> val1;
n = val1;
std::cin >> val1;
p = val1;
}
inline int Crypt::calculate()
{
int val = 0;
/* need to fix casts/variable types */
for (k = 0; k < p; k++) {
val = static_cast<int> (std::pow(static_cast<double>(k), static_cast<double>(n)));
if (val == p) {
return k;
}
}
return -1;
}
What I'm trying to do in this code segment:
Code:
Crypt(std::istream &is): strm(is), n(0), p(0), k(0)
{
}
Is to initialise strm to the value sent in by is. I'm not sure why it's complaining about a copy constructor since I used a reference as the argument for the constructor which shouldn't copy anything. And I also used the constructor initialisation list instead of "copying" it in the body block. I'm literally stuck here with no clue on what's going on. Any help would be great
EDIT: Here's the full compiler warning:
Code:
crypt113.cpp: In copy constructor `std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)':
/usr/lib/gcc/i486-slackware-linux/3.4.5/../../../../include/c++/3.4.5/bits/ios_base.h:781: error: `std::ios_base::ios_base(const std::ios_base&)' is private
crypt113.cpp:12: error: within this context