printing with cout in private functions
Mar 4, 2013 at 4:06am UTC
Hello all,
Im having an error im not expecting,
cout is not defined in this scope
1 2 3 4 5 6 7 8
void QueenSolver:: printMe(){
for (int x=0; x<8; x++)
for (int y=0; y<8; y++){
if (spaces[x][y]) cout<<"x" ;
else cout<<"o" ;
}
cout<<"\n" ;
}
I include iostream at the start of the .cpp file. The function printMe is a private void function in the class queensolver. other than this i dont find any errors. but maybe the community knows something i havent learned yet.
Thank you in advanced!
Full source below
Header
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#ifndef QUEENSOLVER_H
#define QUEENSOLVER_H
#include <iostream>
class QueenSolver
{
public :
int spaces[8][8];
QueenSolver();
bool solve(int );
protected :
private :
bool queensSafe();
void printMe();
};
#endif // QUEENSOLVER_H
implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include "QueenSolver.h"
#include <iostream>
QueenSolver::QueenSolver()
{
for (int x=0 ; x<64; x++)
spaces[x/8][x%8]=0;
}
bool QueenSolver::solve(int x){
for (int q=0; q<8; q++){
spaces[x][q]=1;
if (x==7 && queensSafe()){
printMe();
return true ;
}
else if (queensSafe()){
return solve(x+1);
}
else
spaces[x][q]=0;
}
}
bool QueenSolver::queensSafe(){
//good rows
for (int x=0; x<7; x++){
bool thisRow=false ;
for (int y=0; y<7; y++)
{
if (thisRow && spaces[x][y])
return false ;
}
}
//good cols
for (int x=0; x<7; x++){
bool thisRow=false ;
for (int y=0; y<7; y++)
{
if (thisRow && spaces[y][x])
return false ;
}
}
//good diagonal
return true ;
}
void QueenSolver:: printMe(){
for (int x=0; x<8; x++)
for (int y=0; y<8; y++){
if (spaces[x][y]) cout<<"x" ;
else cout<<"o" ;
}
cout<<"\n" ;
}
runner
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
#include "QueenSolver.cpp"
using namespace std
int main(){
QueenSolver q=new QueenSolver();
q.solve();
return 0;
}
Last edited on Mar 4, 2013 at 4:14am UTC
Mar 4, 2013 at 4:12am UTC
cout resides in the std namespace.
Mar 4, 2013 at 4:16am UTC
would i then call std.cout in order for the function to find the correct cout? or is there another way to achieve what im trying to?
ive worked alot in java this is my first tackle at cpp
Mar 4, 2013 at 4:18am UTC
Use std::cout and it should work.
Mar 4, 2013 at 4:21am UTC
thank you zhuge
Topic archived. No new replies allowed.