|
-
Dec 11th, 2003, 06:40 PM
#1
Thread Starter
Hyperactive Member
Why do I get these warnings in GCC 3.3 and yet compiles fine on vs.net.:Resolved:.
GCC 3.3 compiler in a unix environment I get these warnings
Code:
homework5.cpp: In function `void isMagic(int*, int (*)[4], int, int)':
homework5.cpp:123: warning: ISO C++ forbids declaration of `magicCounter' with
no type
homework5.cpp:179: warning: name lookup of `row' changed for new ISO `for'
scoping
homework5.cpp:135: warning: using obsolete binding at `row'
The reason i'm upset, is because my programming teacher is giving me a 55/100 on this homework assignment because he says, it doesn't compile. Thats bull**** because I just tested it and I just get warnings, not errors. I just need somthing to say to him to show him that, no i'm right, he's wrong, i shouldn't have got 55% on this homework becuase I get 3 warnings on a GCC compiler, and no warnings in .net. He also said, you can use any compiler you want as long as it uses the standard. Here's the program:
Code:
//File: homework5.cpp
//Written By: Cory Sanchez
/*Purpose:
The generatEntries function creates
a one-dimensional array of n2 elements
ordered as the terms of an arithmetic
sequence. The function also outputs the arithmetic
sequence.
The program will then put the one-dimensional matrix
into a two-dimensional matrix and reverse the diagnoals.
A "magic number" is then generated, the program will then
compare the magic number with the sum of each row and column
if they are equal, the matrix has a "magic number".
*/
#include <iostream>
using namespace std;
void generateEntries(int matrix[], int size);
void createMatrix(int matrix[], int newMatrix[][4], int );
void reverseDiagonals(int newMatrix[][4], int row);
void printMatrix(int newMatrix[][4],int size);
void printMatrix(int matrix[], int size);
void isMagic(int matrix[], int newMatrix[][4], int size1, int size2);
int main()
{
const int ROW = 4;
const int COL = 4;
int SIZE = 16; //number of rows * columns == size of whole matrix
int matrix[ROW*COL];
int newMatrix[ROW][COL];
generateEntries(matrix,SIZE);
createMatrix(matrix,newMatrix,ROW);
printMatrix(newMatrix,ROW);
reverseDiagonals(newMatrix,ROW);
printMatrix(newMatrix,ROW);
isMagic(matrix,newMatrix,SIZE,ROW);
return 0;
}
void generateEntries(int matrix[], int size)
{
int first, difference;
cout <<"Enter two numbers: ";
cin >> first >> difference;
matrix[0] = first;
for(int i = 1; i < size; i++)
{
matrix[i] = difference+matrix[i-1];
}
printMatrix(matrix,size);
}
void createMatrix(int matrix[], int newMatrix[][4], int row)
{
static int counter = 0; //to iterate through a of matrix[i]
for(int i = 0; i < row; i++)
{
for(int j = 0; j < row; j++, counter++)
{
newMatrix[i][j] = matrix[counter];
}
}
}
void reverseDiagonals(int newMatrix[][4], int row)
{
int temp;
for(int i = 0; i < row/2; i++)
{
temp = newMatrix[i][i];
newMatrix[i][i] = newMatrix[row-1-i][row-1-i];
newMatrix[row-1-i][row-1-i] = temp;
}
for(int j = 0; j < row/2; j++)
{
temp = newMatrix[j][row-1-j];
newMatrix[j][row-1-j] = newMatrix[row-1-j][j];
newMatrix[row-1-j][j] = temp;
}
}
void printMatrix(int newMatrix[][4],int size)
{
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
{
if(j%4 == 0)
{
cout << endl;
}
cout << newMatrix[i][j]<<" ";
}
}
cout << endl;
}
void printMatrix(int matrix[], int size)
{
for(int i =0; i < size; i++)
{
cout <<matrix[i]<<" ";
}
cout << endl;
}
void isMagic(int matrix[], int newMatrix[][4], int size1, int size2)
{
int sum = 0; //holds the sum of the 1-dim array
int colSum = 0;
int rowSum = 0;
int mainDSum = 0;
int oppDSum = 0;
int magicNum = 0;
static magicCounter = 0; //keeps track of the magic rows/columns
//generating the magic number
for(int i = 0; i < size1; i++)
{
sum += matrix[i];
}
magicNum = sum/size2; //sum divided by # of rows
cout <<"magic number: " << magicNum << endl;
//adding up rows
for(int row = 0; row < size2; row++)
{
rowSum = 0;
for(int col = 0; col < size2; col++)
{
rowSum = rowSum + newMatrix[row][col];
}
if(rowSum == magicNum)
{
magicCounter++;
}
}
//adding up cols
for(int col = 0; col < size2; col++)
{
colSum = 0;
for(int row = 0; row < size2; row++)
{
colSum = colSum + newMatrix[row][col];
}
if(colSum == magicNum)
{
magicCounter++;
}
}
//add up main diagnoal
for(int i = 0; i < size2/2; i++)
{
mainDSum = mainDSum + newMatrix[i][i];
mainDSum += newMatrix[size2-1-i][size2-1-i];
}
if(mainDSum == magicNum)
{
magicCounter++;
}
//add up opposite diagnoal
for(int i = 0; i < size2/2; i++)
{
oppDSum += newMatrix[i][size2-1-i];
oppDSum += newMatrix[row-1-i][i];
}
if(oppDSum == magicNum)
{
magicCounter++;
}
//check if matrix has a magic number
if(magicCounter == (2 * size2+2))
{
cout <<"This matrix has a magic number!\n";
}
else
{
cout <<"This matrix has no magic number!\n";
}
}
Last edited by voidflux; Dec 12th, 2003 at 01:26 PM.
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Dec 11th, 2003, 07:45 PM
#2
Frenzied Member
He must be smoking crack because it just compiled for me with 0 errors or warnings on .NET 2003. Looks good to me execept the static delecration. So I see no reason to 1/2 your grade.
Last edited by Technocrat; Dec 11th, 2003 at 07:52 PM.
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Dec 11th, 2003, 07:48 PM
#3
Hyperactive Member
Have you tried telling him that it compiles fine under VS.NET (which is still pretty standard compliant)? In the future, the best thing to do would be do download GCC and test your programs to make sure they work. This way, you can catch all the little errors you might get in class before they happen.
Good luck convincing your professor.
C.O.M.R.E.A.K.: Cybernetic Obedient Machine Responsible for Exploration and Accurate Killing
-
Dec 11th, 2003, 08:58 PM
#4
Thread Starter
Hyperactive Member
alright thanks guys, i'll tell you how it goes tomarrow afternoon
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Dec 12th, 2003, 05:41 AM
#5
The warnings the compiler gives you are correct,
Code:
static magicCounter = 0;
This variable doesn't have a type (static is not a type, it is a storage-class specifier) you need to declare the variable as
Code:
static int magicCounter = 0;
But you probably didn't mean for it to be static. Static here means that the value of the variable will be preserved with repeated calls to the same function.
Code:
oppDSum += newMatrix[row-1-i][i];
You are using the variable row outside the scope it is declared in. In C++ variables declared in for loops exist only in the loops scope (VS 6 disagrees, don't know about .net)
-
Dec 12th, 2003, 10:31 AM
#6
Thread Starter
Hyperactive Member
I thought in .net if you write static variable name, it assumes the type is integer.
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Dec 12th, 2003, 12:18 PM
#7
Monday Morning Lunatic
Assuming a type is int is a hold-off from older C. Implicit int is removed in later versions of the C standard.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 12th, 2003, 01:19 PM
#8
Thread Starter
Hyperactive Member
ahhh, I feel much better, now my grade changed from a 55/100, to a 100/100! He shouldn't have said, "we can use microsoft compiler", he doesn't talk english well. Another odd thing I found out about GCC, when you overload an operator such as + or -, it will throw up errors all over the place if you try a statement like this:
assume p1 and p2 are objects of class polynomial
cout <<p1<<"+"<<p2 <<"="<< p1+p2 <<endl;
to fix that problem you have to do this which just seems like more code to me:
polynomial x = p1+p2;
cout <<p1<<"+"<<p2 <<"="<< x <<endl;
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Dec 13th, 2003, 10:20 AM
#9
Have you tried
cout <<p1<<"+"<<p2 <<"="<< (p1+p2) <<endl;
?
But actually your teacher is correct. I don't think VC++ would compile your code with the /Za (ANSI enforcement) switch, neither would GCC with a similar switch (-ansi I think). The first two warnings would be errors then.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 13th, 2003, 09:45 PM
#10
Thread Starter
Hyperactive Member
Why would you not allow,
Code:
cout << p1+p2<<endl;
whats so dangerous about this code?
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Dec 14th, 2003, 11:54 AM
#11
Monday Morning Lunatic
Perhaps operator+ isn't defined correctly.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 14th, 2003, 12:12 PM
#12
Bitshift has lower precedence than +, so actually it should work. But who knows...
I always use parentheses on things between cout's <<.
What errors does it give you anyway?
And what's the declaration and implementation of operator + of polynomial?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 14th, 2003, 01:43 PM
#13
Thread Starter
Hyperactive Member
I don't get any errors on .net environment, its when I use GCC 3.1 I get tons of errrors, here's the class definition and declaration of the overloaded +/- operators and the << operators
assume class Polynomial, which has 2 data members, int a, b, c;
Code:
Polynomial operator+ (const Polynomial& p1);
Polynomial operator- (const Polynomial& p1);
friend ostream &operator<<(ostream &out, Polynomial& p1);
ostream &operator<<(ostream &out, Polynomial &p1)
{
if(p1.b >0 && p1.c>=0)
{
out <<p1.a<<"x^2+"<<p1.b<<"x+"<<p1.c;
}
else if(p1.b<0 && p1.c<0)
{
out <<p1.a<<"x^2"<<p1.b<<"x"<<p1.c;
}
else if(p1.b<0 && p1.c>=0)
{
out <<p1.a<<"x^2"<<p1.b<<"x+"<<p1.c;
}
else
{
out <<p1.a<<"x^2+"<<p1.b<<"x"<<p1.c;
}
return out;
}
Polynomial Polynomial::operator+ (const Polynomial& p1)
{
Polynomial p3; //holds the new value of calling object + another object
p3.a = a + p1.a;
p3.b = b + p1.b;
p3.c = c + p1.c;
return p3;
}
Polynomial Polynomial::operator- (const Polynomial& p1)
{
Polynomial p3;
p3.a = a - p1.a;
p3.b = b - p1.b;
p3.c = c - p1.c;
return p3;
}
Here's just a tad of the errors I get:
Code:
test.cpp: In member function `double Polynomial::getBiggerRoot()':
test.cpp:52: error: call of overloaded `sqrt(int)' is ambiguous
/usr/include/architecture/ppc/math.h:249: error: candidates are: double
sqrt(double)
/usr/include/gcc/darwin/3.3/c++/cmath:465: error: long double
std::sqrt(long double)
/usr/include/gcc/darwin/3.3/c++/cmath:461: error: float
std::sqrt(float)
test.cpp: In member function `double Polynomial::getSmallerRoot()':
test.cpp:73: error: call of overloaded `sqrt(int)' is ambiguous
/usr/include/architecture/ppc/math.h:249: error: candidates are: double
sqrt(double)
/usr/include/gcc/darwin/3.3/c++/cmath:465: error: long double
std::sqrt(long double)
/usr/include/gcc/darwin/3.3/c++/cmath:461: error: float
std::sqrt(float)
test.cpp: In function `int main()':
test.cpp:257: error: no match for `std::basic_ostream<char,
std::char_traits<char> >& << Polynomial' operator
/usr/include/gcc/darwin/3.3/c++/bits/ostream.tcc:63: error: candidates are:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(std::basic_ostream<_CharT,
_Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char,
_Traits = std::char_traits<char>]
/usr/include/gcc/darwin/3.3/c++/bits/ostream.tcc:85: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(std::basic_ios<_CharT,
_Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits
= std::char_traits<char>]
/usr/include/gcc/darwin/3.3/c++/bits/ostream.tcc:107: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT = char,
_Traits = std::char_traits<char>]
/usr/include/gcc/darwin/3.3/c++/bits/ostream.tcc:179: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(long int) [with _CharT = char, _Traits =
std::char_traits<char>]
The errrors go on for about 5 pages!
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Dec 14th, 2003, 02:58 PM
#14
The first few errors are about the sqrt function. You are applying it to ints, but the compiler is unsure whether to use the double or the float version of sqrt. An explicit static_cast<float>(some_int_value) would fix this error.
The other errors are caused by your operator <<. It takes a Polynomial&, but according to the standard you can't take a reference to a temporary object. The values returned by operator + are temporary. A solution would be to let the operator << take a const Polynomial&
-
Dec 14th, 2003, 04:09 PM
#15
Thread Starter
Hyperactive Member
excellent! thanks for clearing that up!
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|