[RESOLVED] My First Attempt at Header Files
So, after hitting Chapter 4 of Accelerated C++, I decided to test my knowledge and take example 4-3 and extend it to using custom header files, etc... however, I'm getting a whole slew of build errors from it... so, here's the errors:
Quote:
1>------ Build started: Project: Squared, Configuration: Debug Win32 ------
1>Compiling...
1>Squared.cpp
(11) : error C2143: syntax error : missing ';' before '<'
(11) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
(12) : error C2062: type 'int' unexpected
squareio.h(11) : error C2065: 'string' : undeclared identifier
squareio.h(11) : error C2182: 'Pause' : illegal use of type 'void'
squareio.h(13) : error C2062: type 'int' unexpected
squareio.h(13) : error C2653: 'string' : is not a class or namespace name
squared.cpp(16) : error C2872: 'vector' : ambiguous symbol
1> could be 'c:\documents and settings\bleas\my documents\c++\projects\accelerated c++ projects\chapter 4\squared\squared\squaremath.h(11) : int vector'
1> or 'c:\program files\microsoft visual studio 9.0\vc\include\vector(438) : std::vector'
squared.cpp(16) : error C2872: 'vector' : ambiguous symbol
1> could be 'c:\documents and settings\bleas\my documents\c++\projects\accelerated c++ projects\chapter 4\squared\squared\squaremath.h(11) : int vector'
1> or 'c:\program files\microsoft visual studio 9.0\vc\include\vector(438) : std::vector'
squared.cpp(16) : error C2872: 'vector' : ambiguous symbol
1> could be 'c:\documents and settings\bleas\my documents\c++\projects\accelerated c++ projects\chapter 4\squared\squared\squaremath.h(11) : int vector'
1> or 'c:\program files\microsoft visual studio 9.0\vc\include\vector(438) : std::vector'
squared.cpp(16) : error C2872: 'vector' : ambiguous symbol
1> could be 'c:\documents and settings\bleas\my documents\c++\projects\accelerated c++ projects\chapter 4\squared\squared\squaremath.h(11) : int vector'
1> or 'c:\program files\microsoft visual studio 9.0\vc\include\vector(438) : std::vector'
squared.cpp(16) : error C2062: type 'int' unexpected
squared.cpp(20) : error C2065: 'stored_squares' : undeclared identifier
squared.cpp(20) : error C2065: 'stored_squares' : undeclared identifier
squared.cpp(20) : error C3861: 'GetSquares': identifier not found
squared.cpp(21) : error C2065: 'stored_squares' : undeclared identifier
squared.cpp(21) : error C3861: 'LongestSquare': identifier not found
squared.cpp(22) : error C2065: 'stored_squares' : undeclared identifier
squared.cpp(22) : error C3861: 'DisplaySquares': identifier not found
squared.cpp(24) : error C2064: term does not evaluate to a function taking 1 arguments
1>SquareMath.cpp
(11) : error C2143: syntax error : missing ';' before '<'
(11) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
(12) : error C2062: type 'int' unexpected
squaremath.cpp(9) : error C2143: syntax error : missing ';' before '<'
squaremath.cpp(9) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
squaremath.cpp(9) : error C2086: 'int vector' : redefinition
1> c:\documents and settings\bleas\my documents\c++\projects\accelerated c++ projects\chapter 4\squared\squared\squaremath.h(11) : see declaration of 'vector'
squaremath.cpp(10) : error C2143: syntax error : missing ';' before '{'
squaremath.cpp(10) : error C2447: '{' : missing function header (old-style formal list?)
squaremath.cpp(21) : error C2653: 'string' : is not a class or namespace name
squaremath.cpp(21) : error C2146: syntax error : missing ';' before identifier 'LongestSquare'
squaremath.cpp(21) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
squaremath.cpp(21) : error C2062: type 'int' unexpected
squaremath.cpp(22) : error C2143: syntax error : missing ';' before '{'
squaremath.cpp(22) : error C2447: '{' : missing function header (old-style formal list?)
1>SquareIO.cpp
squareio.h(11) : error C2065: 'string' : undeclared identifier
squareio.h(11) : error C2182: 'Pause' : illegal use of type 'void'
squareio.h(13) : error C2065: 'vector' : undeclared identifier
squareio.h(13) : error C2062: type 'int' unexpected
squareio.h(13) : error C2653: 'string' : is not a class or namespace name
squareio.cpp(9) : error C2065: 'string' : undeclared identifier
squareio.cpp(9) : error C2146: syntax error : missing ')' before identifier 'Message'
squareio.cpp(9) : error C2182: 'Pause' : illegal use of type 'void'
squareio.cpp(9) : error C2374: 'Pause' : redefinition; multiple initialization
1> c:\documents and settings\bleas\my documents\c++\projects\accelerated c++ projects\chapter 4\squared\squared\squareio.h(11) : see declaration of 'Pause'
squareio.cpp(9) : error C2059: syntax error : ')'
squareio.cpp(10) : error C2143: syntax error : missing ';' before '{'
squareio.cpp(10) : error C2447: '{' : missing function header (old-style formal list?)
squareio.cpp(18) : error C2065: 'cout' : undeclared identifier
squareio.cpp(20) : error C2065: 'endl' : undeclared identifier
squareio.cpp(21) : error C2065: 'cin' : undeclared identifier
squareio.cpp(29) : error C2065: 'vector' : undeclared identifier
squareio.cpp(29) : error C2062: type 'int' unexpected
squareio.cpp(29) : error C2653: 'string' : is not a class or namespace name
squareio.cpp(30) : error C2143: syntax error : missing ';' before '{'
squareio.cpp(30) : error C2447: '{' : missing function header (old-style formal list?)
1>Generating Code...
1>Build log was saved at "file://c:\Documents and Settings\bleas\My Documents\C++\Projects\Accelerated C++ Projects\Chapter 4\Squared\Squared\Debug\BuildLog.htm"
1>Squared - 54 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Here's the code:
Squared.cpp
Code:
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include <vector>
#include <stdio.h>
#include "SquareMath.h"
#include "SquareIO.h"
//#include "Safe_Input.h"
using namespace std;
int main ()
{
int max_square = 0, _LongestSquare;
vector<int> stored_squares;
string::size_type Longest;
max_square = GetMaxSquares(max_square);
stored_squares = GetSquares(stored_squares, max_square);
Longest = LongestSquare(stored_squares, max_square, _LongestSquare);
DisplaySquares(stored_squares, max_square, Longest,_LongestSquare);
Pause("Press any key to exit Squared");
return 0;
}
SquareMath.h
Code:
#ifndef GUARD_SQUAREMATH
#define GUARD_SQUAREMATH
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include <vector>
#include <stdio.h>
vector<int> GetSquares(vector<int>, int);
std::string::size_type LongestSquare(vector<int>, int, int&);
#endif //GUARD SQUAREMATH
SquareMath.cpp
Code:
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include <vector>
#include <stdio.h>
#include "SquareMath.h"
vector<int> GetSquares(vector<int> stored_squares, int max_square)
{
int run_counter = 0;
while (run_counter != max_square)
{
int curr_square = (run_counter+1) * (run_counter+1);
stored_squares.push_back(curr_square);
++run_counter;
}
return stored_squares;
}
string::size_type LongestSquare(vector<int> stored_squares, int max_square, int& _LongestSquare)
{
vector<int> Copy_Of_stored_squares = stored_squares;
string::size_type Longest = 0;
string CompareString;
int run_counter = 0;
while (run_counter != max_square)
{
stringstream out;
out << stored_squares[run_counter];
CompareString = out.str();
if (CompareString.size() > Longest)
{
Longest = CompareString.size();
_LongestSquare = Copy_Of_stored_squares[run_counter];
}
++run_counter;
}
return Longest;
}
SquareIO.h
Code:
#ifndef GUARD_SQUAREIO
#define GUARD_SQUAREIO
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include <vector>
#include <stdio.h>
void Pause(string);
int GetMaxSquares(int);
void DisplaySquares(vector<int>, int, string::size_type, int);
#endif //GUARD_SQUAREIO
SquareIO.cpp
Code:
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include <vector>
#include <stdio.h>
#include "SquareIO.h"
void Pause(string Message = "\nPress any key to Continue\n")
{
cout << Message;
cin.sync();
getchar();
}
int GetMaxSquares(int max_square)
{
cout << "How many numbers do you wish to square?"
<< endl
<< "> ";
cin >> max_square;
//max_square = GetIntInput("How many numbers do you wish to square?\n> ");
return max_square;
}
void DisplaySquares(vector<int> stored_square, int max_square, string::size_type Longest, int _LongestSquare)
{
string CompareString;
string::size_type Length;
stringstream out;
out << _LongestSquare;
CompareString = out.str();
Length = CompareString.size();
const int Max_Column_Width = Longest + Length + 4 + 2;
const int Max_Column_Height = max_square + 2;
int BreakPoint = 0;
for(int Current_Row = 0; Current_Row != Max_Column_Height; ++Current_Row)
{
if (BreakPoint == 19)
{
Pause();
BreakPoint = 0;
}
int Current_Column = 0;
while(Current_Column != Max_Column_Width)
{
if (Current_Row == 0 || Current_Row == Max_Column_Height - 1)
{
cout << "-";
++Current_Column;
}
else
{
if(Current_Column == 0 || Current_Column == Max_Column_Width - 1)
{
cout << "|";
++Current_Column;
}
else if (Current_Column == 1)
{
cout << setfill(' ') << setw(Longest)
<< Current_Row;
Current_Column += Longest;
}
else if (Current_Column == Longest + 4)
{
cout << setfill(' ') << setw(Length)
<< stored_square[Current_Row - 1];
Current_Column += Length;
}
else
{
cout << ' ';
++Current_Column;
}
}
}
cout << endl;
++BreakPoint;
}
}
What boggles me is that it has no errors when it's all in Squared.cpp, so it's not as if I had lingering problems to begin with. Ah, well. Anyways, feel free to comment on bits of the code itself, but keep in mind that I'm only on Chapter 4 of Accelerated C++. I'd love any help I could get.
Re: My First Attempt at Header Files
In the cpp files, you write "using namespace std;", but it's inappropriate to do that in the headers: there you need to fully-qualify everything, std::vector, etc.
Re: My First Attempt at Header Files
I was thinking that shortly after I posted, but the confirmation helped. I feel a bit stupid now...