Interesting C++ operator overloading problem [closed] - c++

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I made a Matrix class on my own. but there is interesting problem when overloading + operator.
Matrix Matrix::operator+ (Matrix& operand)
{
if(row_size == operand.row_size && col_size == operand.col_size)
{
Matrix temp(row_size, col_size);
for(int i = 0; i < col_size; i ++)
{
for(int j = 0; j < row_size; j ++)
{
temp[i][j] = data_list[i][j] + operand.data_list[i][j];
}
}
return temp;
}
else
throw Error::Matrix_error(0);
}
When I do
matrix + matrix2;
on main function, it seems like the program is in a infinite loop. So, I just debugged it and found out that the code stops at return temp; part.
I have no idea why this function cannot return temp since return type of operator+ is Matrix.
Plus, this code totally works well when I compile in a RELEASE mode (visual studio 2010). However it does not work when I compile in a DEBUG mode.

The problem was I miss created copy constructor. It looks like it fell on infinite loop at copy constructor but the debugger didn't catch it.

Related

C++ code: count not working [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I wanted to ask, i formulated this code to solve a question, but count does not seem to provide the right value.
Any advice.Any help appreciated. Thanks.
#include<iostream.h>
#include<conio.h>
void main()
{
int count;
for(int a=1;a<125;a++)
for(int m=1;m<125;m++)
for(int n=1;n<125;n++)
{
if(a*(m+n+2)==249-m)
{
cout<<"a = "<<a<<" m = "<<m<<" n = "<<n<<"\n";
count=count+1;
}
}
cout<<"count = "<<count<<"\n";
getch();
}
You do not init the count. Remember to set int count = 0;.
Your compiler will warn you about this and save you the trouble of debugging or asking if you only let it. (from #chris)

Loop without for, while or goto [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
In Windows, one can use structured exception handling in C to write a pseudo-loop that prints all numbers from 1 to 1000 like this:
int n = 0;
__try {
*(int *)0 = 0;
}
__except(printf("%i\n", ++n), n < 1000 ? -1 : 1) {
}
I wonder if there are also other ways in C/C++ to create a loop that isn't trivial to detect if you search the code for the usual suspect keywords for, while and goto.
In C++, a simple lambda can do that:
std::function<void(int,int)> print = [&](int from, int to)
{
std::cout << from << " ";
if ( from < to ) print(++from, to);
};
print(1, 1000);
It will print all integers from 1 to 1000. And it doesn't use for, while or goto.

replace function in std::string giving problems in C++ [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am using C++ string functions in cocos2dx. I have the following string CorrectAns = "below".
for(int i = 0; i<CorrectAns.size();i++)
{
CorrectAns.replace(i,i,"?");
}
This function should return my string as "?????", but its returning only 4 charcters ie "????".
When I write like this,
for(int i = 0; i<CorrectAns.size();i++)
{
if(i == 0)
{
CorrectAns.replace(i,i,"?");
}
}
It just crashes.
and works fine only when I write it as " CorrectAns.replace(i,i+1,"?");"
Why is the function working this way?? Can anyone help me please??
string& replace ( size_t pos1, size_t n1, const string& str );
For the versions with parameters pos1 and n1, the section replaced
begins at character position pos1 and spans for n1 characters within
the string.
So you should use
for(int i = 0; i<CorrectAns.size();i++)
{
CorrectAns.replace(i,1,"?");
}
Mb it will be more usefull use something like
CorrectAns.assign(CorrectAns.size(), '?');

C++ weird error with variable not being declared in scope, when it clearly is [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm currently trying use a very simple check using the variable "breakOK", in order to break out of a for loop. If the condition that the current value in my puzzle matrix is set to 46 is satisfied, a function is called and breakOK is set to 1 so that we can break out of the loop. However I get the following errors where I use breakOK in the if statements of the code below:
sudoku.cc:482:13: error: 'breakOK' was not declared in this scope
sudoku.cc:485:11: error: 'breakOK' was not declared in this scope
This is really weird because I declare Guess in the same manner and I do not get a scope error when using it at a later time in my code! Also, the compiler doesn't complain when I set breakOK equal to 1 in the loop. Any help would be much appreciated, I've been stuck on this for too long!
int Guess = 0; // will be set to a value if a guess is ok
int breakOk = 0; // will be set to 1 if breaking out of loops is necessary
for (int row = 0; row < 9; row++)
{
for (int col = 0; col < 9; col++)
{
if (puzzle.matrix[row][col].currentValue == 46)
{
Guess = guessValues(row, col, puzzle, tried, allowed);
breakOk = 1;
}
if (Guess != 0)
{
tried[row][col].tries.push_back(Guess);
//puzzle.decide(row, col, Guess);
puzzle.matrix[row][col].currentValue = Guess;
}
if (breakOK == 1) // line 482
break;
}
if (breakOK == 1) // line 485
break;
}
You declare breakOk, but check breakOK. Notice the capital K in OK.
It's because your variable is breakOk and you are referencing breakOK
breakOK is not the same as breakOk. Check your case on the k.

function keeps returning NaN [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
Hi guys i know what NaN(let me say i know the acronym stands for Not a Number) is but i don't understand why C++ returns it - The following is the approximation of the mathematical constant e - When using the debugger the functions evaluate fine, it's when writing to the console that it returns NaN
Thanks for any feedback
double Factorial(int k)
{
if(k == 0)
return 1;
int value = 1;
for(int i = k; i > 0; i--)
value *= k;
return value;
}
double e(int p)
{
double value = 0.0;
for(int i = 0; i < p; i++)
{
value += 1/Factorial(i);
}
}
You don't return a value in your e function.
You forgot to return value at the end of e. I don't know when c++ stopped warning about missing returns.