我不明白的错误

时间:2014-04-11 18:20:57

标签: c++

我编译时遇到此错误,但我不确定如何修复它。我发现它与尝试使用ofstream outFile有关;写入文件。我已经用它做了,但我不确定是什么问题。

1>Rectangle.obj : error LNK2001: unresolved external symbol "class std::basic_ofstream<char,struct std::char_traits<char> > outFile" (?outFile@@3V?$basic_ofstream@DU?$char_traits@D@std@@@std@@A)
1>E:\Hw11_overload_Hw8\Debug\Rectangle.exe : fatal error LNK1120: 1 unresolved externals

这是我的头文件。

#ifndef Rectangle_h
#define Rectangle_h

#include <iostream>  
#include <fstream>
#include <sstream>

using namespace std;

class Rectangle
{

friend istream &operator>> (istream &input, Rectangle &grid);
friend ostream &operator<< (ostream &output, Rectangle grid);

private:
int i, j, h, x1, x2, y1, y2, perimeter, area, width, height;
char inner, outer;
string name;

public:
Rectangle();

void printGrid();

void setX1(int x11);
int getX1();
void setX2(int x21);
int getX2();
void setY1(int y11);
int getY1();
void setY2(int y21);
int getY2();

void setInner(char inner1);
char getInner();
void setOuter(char outer1);
char getOuter();

void findPerimeter();
void findArea();
void findWidth();
void findHeight();

void setName(string name1);
string getName();

int Menu();

};

#endif

这是我的源文件。

#include "Rectangle.h"

extern ofstream outFile;

Rectangle::Rectangle()
{
x1 = 0;
x2 = 1;
y1 = 0;
y2 = 1;
}

void Rectangle::setX1(int x11)
{
x1 = x11;
}

int Rectangle::getX1()
{
return x1;
}

void Rectangle::setX2(int x21)
{
x2 = x21;
}

int Rectangle::getX2()
{
return x2;
}

void Rectangle::setY1(int y11)
{
y1 = y11;
}

int Rectangle::getY1()
{
return y1;
}

void Rectangle::setY2(int y21)
{
y2 = y21;
}

int Rectangle::getY2()
{
return y2;
}

void Rectangle::findPerimeter()
{
perimeter = ((x2 - x1) + (y2 - y1)) * 2;
cout << "The perimeter of the rectangle is: " << perimeter << endl;
}

void Rectangle::findArea()
{
area = (x2 - x1) * (y2 - y1);
cout << "The area of the rectangle is: " << area << endl;
}

void Rectangle::findWidth()
{
width = x2 - x1;
cout << "The width of the rectangle is: " << width << endl;
}

void Rectangle::findHeight()
{
height = y2 - y1;
cout << "The height of the rectangle is: " << height << endl;
}

void Rectangle::setInner(char inner1)
{
    inner = inner1;
}
char Rectangle::getInner()
{
return inner;
}

void Rectangle::setOuter(char outer1)
{
outer = outer1;
}
char Rectangle::getOuter()
{
return outer;
}

void Rectangle::setName(string name1)
{
name = name1;
}

string Rectangle::getName()
{
return name;
}

void Rectangle::printGrid()
{
int numrows = 25;
int numcols = 25;
int current_row = numrows; // starting row number
int current_col = 1; // starting col number
char output = '.';

for(i = 0; i < numrows; i++)        // prints 25 rows of 25 dots 
{   

    cout << current_row << '\t';    // Print out current row number
    outFile << current_row << '\t';

    //This is out loop for each ROW
    //Print out dots in each row OR stuff for the rectangle
    for(j = 1; j <= numcols; j++) {

        output = '.'; // Initialize output with our default value of "."


        if ((current_col >= x1) && (current_col <= x2) && (current_row >=    y1) && (current_row <= y2)) {
            output = outer;
        }

        if ((current_col > x1) && (current_col < x2) && (current_row > y1) && (current_row < y2)) {
            output = inner;
        }

        cout << output << "  "; // output our "output" value and a space            
        outFile << output << "  ";
        current_col++;  //Increment current column, because this is the end of this column loop iteration

    } // Close column loop

    cout << endl;       //...and a new line
    outFile << endl;

    current_col = 1;    // reset column count for next iteration
    current_row--;      // decrement current row number

} // Close Row loop


//output column labels across bottom line
cout << '\t';
outFile << '\t';

// put 1 -> 25 across the bottom
for (i = 1; i <= 25; i++)
{
    if(i < 10)
    {
        cout << i << "  ";
        outFile << i << "  ";
    }

    if(i > 9)
    {
        cout << i << " ";
        outFile << i << " ";
    }
}

// Spit out a couple of blank lines
cout << endl << endl;
outFile << endl << endl;
}

int Rectangle::Menu()
{
ifstream inFile;

int choice;

cout << "1. Enter information about a rectangle. " << endl;
cout << "2. Search for a rectangle. " << endl;
cout << "3. Print the perimeter and the area of the rectangle. " << endl;
cout << "4. Print the width and height of the rectangle. " << endl;
cout << "5. Draw a particular rectangle. " << endl;
cout << "6. Quit. " << endl;
cout << "Enter your choice. " << endl;
inFile >> choice;
cout << endl << endl;

return choice;
}

istream &operator>> (istream &input, Rectangle &grid)
{
ifstream inFile;
ofstream outFile;

Rectangle rect[10];
int x11, x21, y11, y21, choice, numRectangles = 0, i = 0;
char inner1, outer1;
string name1;

inFile.open ("rectangle.in");
outFile.open ("rectangle.out");

if (!inFile)
{
    cout << "ERROR:  inFile does not exist. " << endl;
    system("pause");
}

if (!outFile)
{
    cout << "ERROR:  outFile does not exist. " << endl;
    system("pause");
}

choice = rect[i].Menu();

while(choice >= 1 && choice <= 6)
{
    if(choice == 1)
    {   do
        {
            cout << "Enter x1, x2, y1, and y2 such that x1 < x2 and y1 <  y2: " << endl;
            inFile >> x11;
            inFile >> x21;
            inFile >> y11;
            inFile >> y21;
            cout << "Enter a character for the interior of the   rectangle. \n";
            inFile >> inner1;
            cout << "Enter a character for the exterior of the rectangle. \n";
            inFile >> outer1;
            cout << "Enter a name for the rectangle. \n";
            inFile >> name1;

            rect[numRectangles].setX1(x11);
            rect[numRectangles].setX2(x21);
            rect[numRectangles].setY1(y11);
            rect[numRectangles].setY2(y21);
            rect[numRectangles].setInner(inner1);
            rect[numRectangles].setOuter(outer1);
            rect[numRectangles].setName(name1);

            numRectangles++;
        }
        while(x11 >= x21 || y11 >= y21);
            cout << endl << "Rectangle accepted. \n" << endl;
    }

if (choice == 2)
    {
        cout << "To search for a rectangle, enter the name of the rectangle.  " << endl;
        cin >> name1;

        for(i = 0; i < numRectangles; i++)
            {
                if(name1 == rect[i].getName())
                {
                    rect[i].findPerimeter();
                    rect[i].findArea();
                    rect[i].findWidth();
                    rect[i].findHeight();
                    rect[i].printGrid();
                    system("pause");
                }
            }
        if(name1 != rect[i].getName())
            {
                cout << "ERROR! Rectangle doesn't exist!" << endl <<  endl;
                system("pause");
            }
    }

    if(choice == 3)
    {
        for(i = 0; i < numRectangles; i++)
        {
            rect[i].findPerimeter();
            cout << endl;
            rect[i].findArea();
            cout << endl;
        }
    }

    if(choice == 4)
    {
        for(i = 0; i < numRectangles; i++)
            {
                rect[i].findWidth();
                cout << endl;
                rect[i].findHeight();
                cout << endl;
            }
    }   

    if(choice == 5)
    {
        for(i = 0; i < numRectangles; i++)
        {
            rect[i].printGrid();
            cout << endl;
        }
    }

    if(choice == 6)
        {
            cout << "Bye bye!" << endl;
            system("pause");
        }

    choice = rect[i].Menu();
    return input;

}   // End of while loop

inFile.close();
outFile.close();

}

ostream &operator<< (ostream &output, Rectangle grid)
{
return output;
}

这是我的主要功能

#include "Rectangle.h"

int main()

Rectangle rect[10];

int numRectangles;

for(int i = 0; i < numRectangles; i++)
{
    cin >> rect[i];
    cout << rect[i];
}

}

1 个答案:

答案 0 :(得分:0)

问题:

您已声明outFile将具有外部链接。 std::ofstream没有默认构造函数。

std::ofstream有一个非默认的构造函数并且有两个参数..第一个是文件的位置,第二个(默认是btw)是你计划打开/写入它的方式。

因此,您可以将extern ofstream outFile;替换为:

来修复此错误 ofstream outFile("output.txt");

中的

Rectangle.cpp

据我所知,没有必要在extern文件中将其声明为.cpp


接下来,istream& operator>> (istream& input, Rectangle& grid)正在返回NOTHING ..

应该在函数完成之前返回input


最后,您在主()括号后缺少大括号。

main.cpp应该是这样的:

#include "Rectangle.h"

int main()
{
    Rectangle rect[10];

    int numRectangles;

    for(int i = 0; i < numRectangles; i++)
    {
        cin >> rect[i];
        cout << rect[i];
    }
}

遵循约定并格式化代码真的会让你失望吗?

如果操作正确,您的代码将如下所示:

Rectangle.h:

#ifndef Rectangle_h
#define Rectangle_h

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

class Rectangle
{
    private:
        friend istream& operator>> (istream& input, Rectangle& grid);
        friend ostream& operator<< (ostream& output, Rectangle grid);

        int i, j, h, x1, x2, y1, y2, perimeter, area, width, height;
        char inner, outer;
        string name;

    public:
        Rectangle();

        void printGrid();

        void setX1(int x11);
        int getX1();
        void setX2(int x21);
        int getX2();
        void setY1(int y11);
        int getY1();
        void setY2(int y21);
        int getY2();

        void setInner(char inner1);
        char getInner();
        void setOuter(char outer1);
        char getOuter();

        void findPerimeter();
        void findArea();
        void findWidth();
        void findHeight();

        void setName(string name1);
        string getName();

        int Menu();

};

#endif

Rectangle.cpp:

#include "Rectangle.h"

ofstream outFile("output.txt");

Rectangle::Rectangle()
{
    x1 = 0;
    x2 = 1;
    y1 = 0;
    y2 = 1;
}

void Rectangle::setX1(int x11)
{
    x1 = x11;
}

int Rectangle::getX1()
{
    return x1;
}

void Rectangle::setX2(int x21)
{
    x2 = x21;
}

int Rectangle::getX2()
{
    return x2;
}

void Rectangle::setY1(int y11)
{
    y1 = y11;
}

int Rectangle::getY1()
{
    return y1;
}

void Rectangle::setY2(int y21)
{
    y2 = y21;
}

int Rectangle::getY2()
{
    return y2;
}

void Rectangle::findPerimeter()
{
    perimeter = ((x2 - x1) + (y2 - y1)) * 2;
    cout << "The perimeter of the rectangle is: " << perimeter << endl;
}

void Rectangle::findArea()
{
    area = (x2 - x1) * (y2 - y1);
    cout << "The area of the rectangle is: " << area << endl;
}

void Rectangle::findWidth()
{
    width = x2 - x1;
    cout << "The width of the rectangle is: " << width << endl;
}

void Rectangle::findHeight()
{
    height = y2 - y1;
    cout << "The height of the rectangle is: " << height << endl;
}

void Rectangle::setInner(char inner1)
{
    inner = inner1;
}
char Rectangle::getInner()
{
    return inner;
}

void Rectangle::setOuter(char outer1)
{
    outer = outer1;
}
char Rectangle::getOuter()
{
    return outer;
}

void Rectangle::setName(string name1)
{
    name = name1;
}

string Rectangle::getName()
{
    return name;
}

void Rectangle::printGrid()
{
    int numrows = 25;
    int numcols = 25;
    int current_row = numrows; // starting row number
    int current_col = 1; // starting col number
    char output = '.';

    for(i = 0; i < numrows; i++)        // prints 25 rows of 25 dots
    {

        cout << current_row << '\t';    // Print out current row number
        outFile << current_row << '\t';

        //This is out loop for each ROW
        //Print out dots in each row OR stuff for the rectangle
        for(j = 1; j <= numcols; j++)
        {

            output = '.'; // Initialize output with our default value of "."


            if ((current_col >= x1) && (current_col <= x2) && (current_row >=    y1) && (current_row <= y2))
            {
                output = outer;
            }

            if ((current_col > x1) && (current_col < x2) && (current_row > y1) && (current_row < y2))
            {
                output = inner;
            }

            cout << output << "  "; // output our "output" value and a space
            outFile << output << "  ";
            current_col++;  //Increment current column, because this is the end of this column loop iteration

        } // Close column loop

        cout << endl;       //...and a new line
        outFile << endl;

        current_col = 1;    // reset column count for next iteration
        current_row--;      // decrement current row number

    } // Close Row loop


//output column labels across bottom line
    cout << '\t';
    outFile << '\t';

// put 1 -> 25 across the bottom
    for (i = 1; i <= 25; i++)
    {
        if(i < 10)
        {
            cout << i << "  ";
            outFile << i << "  ";
        }

        if(i > 9)
        {
            cout << i << " ";
            outFile << i << " ";
        }
    }

// Spit out a couple of blank lines
    cout << endl << endl;
    outFile << endl << endl;
}

int Rectangle::Menu()
{
    ifstream inFile;

    int choice;

    cout << "1. Enter information about a rectangle. " << endl;
    cout << "2. Search for a rectangle. " << endl;
    cout << "3. Print the perimeter and the area of the rectangle. " << endl;
    cout << "4. Print the width and height of the rectangle. " << endl;
    cout << "5. Draw a particular rectangle. " << endl;
    cout << "6. Quit. " << endl;
    cout << "Enter your choice. " << endl;
    inFile >> choice;
    cout << endl << endl;

    return choice;
}

istream& operator>> (istream& input, Rectangle& grid)
{
    ifstream inFile;
    ofstream outFile;

    Rectangle rect[10];
    int x11, x21, y11, y21, choice, numRectangles = 0, i = 0;
    char inner1, outer1;
    string name1;

    inFile.open ("rectangle.in");
    outFile.open ("rectangle.out");

    if (!inFile)
    {
        cout << "ERROR:  inFile does not exist. " << endl;
        system("pause");
    }

    if (!outFile)
    {
        cout << "ERROR:  outFile does not exist. " << endl;
        system("pause");
    }

    choice = rect[i].Menu();

    while(choice >= 1 && choice <= 6)
    {
        if(choice == 1)
        {
            do
            {
                cout << "Enter x1, x2, y1, and y2 such that x1 < x2 and y1 <  y2: " << endl;
                inFile >> x11;
                inFile >> x21;
                inFile >> y11;
                inFile >> y21;
                cout << "Enter a character for the interior of the   rectangle. \n";
                inFile >> inner1;
                cout << "Enter a character for the exterior of the rectangle. \n";
                inFile >> outer1;
                cout << "Enter a name for the rectangle. \n";
                inFile >> name1;

                rect[numRectangles].setX1(x11);
                rect[numRectangles].setX2(x21);
                rect[numRectangles].setY1(y11);
                rect[numRectangles].setY2(y21);
                rect[numRectangles].setInner(inner1);
                rect[numRectangles].setOuter(outer1);
                rect[numRectangles].setName(name1);

                numRectangles++;
            }
            while(x11 >= x21 || y11 >= y21);
            cout << endl << "Rectangle accepted. \n" << endl;
        }

        if (choice == 2)
        {
            cout << "To search for a rectangle, enter the name of the rectangle.  " << endl;
            cin >> name1;

            for(i = 0; i < numRectangles; i++)
            {
                if(name1 == rect[i].getName())
                {
                    rect[i].findPerimeter();
                    rect[i].findArea();
                    rect[i].findWidth();
                    rect[i].findHeight();
                    rect[i].printGrid();
                    system("pause");
                }
            }
            if(name1 != rect[i].getName())
            {
                cout << "ERROR! Rectangle doesn't exist!" << endl <<  endl;
                system("pause");
            }
        }

        if(choice == 3)
        {
            for(i = 0; i < numRectangles; i++)
            {
                rect[i].findPerimeter();
                cout << endl;
                rect[i].findArea();
                cout << endl;
            }
        }

        if(choice == 4)
        {
            for(i = 0; i < numRectangles; i++)
            {
                rect[i].findWidth();
                cout << endl;
                rect[i].findHeight();
                cout << endl;
            }
        }

        if(choice == 5)
        {
            for(i = 0; i < numRectangles; i++)
            {
                rect[i].printGrid();
                cout << endl;
            }
        }

        if(choice == 6)
        {
            cout << "Bye bye!" << endl;
            system("pause");
        }

        choice = rect[i].Menu();
        return input;

    }   // End of while loop

    inFile.close();
    outFile.close();
    return input;
}

ostream& operator<< (ostream& output, Rectangle grid)
{
    return output;
}

main.cpp中:

#include "Rectangle.h"

int main()
{
    Rectangle rect[10];

    int numRectangles;

    for(int i = 0; i < numRectangles; i++)
    {
        cin >> rect[i];
        cout << rect[i];
    }
}

除了让它编译之外,我没有检查任何其他内容。这意味着我不确定你是否有任何错误阻止它正常运行。我还建议您避免在头文件中使用using namespace std;