C++ 一个我不知道的错误';我不明白
我在编译时遇到这个错误,但我不确定如何修复它。我发现这和试图使用流出物有关;写入文件。我把它玩弄了,但我不确定问题出在哪里C++ 一个我不知道的错误';我不明白,c++,C++,我在编译时遇到这个错误,但我不确定如何修复它。我发现这和试图使用流出物有关;写入文件。我把它玩弄了,但我不确定问题出在哪里 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_t
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
1>Rectangle.obj:错误LNK2001:未解析的外部符号“class std::basic_of stream outFile”(?outFile@@3V?$basic)_ofstream@DU?$char_traits@D@std@@@std@@A)
1> E:\Hw11\u重载\u Hw8\Debug\Rectangle.exe:致命错误LNK1120:1未解析的外部
这是我的头文件
#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
#如果为矩形#
#定义矩形
#包括
#包括
#包括
使用名称空间std;
类矩形
{
friend istream&operator>>(istream&input、矩形和网格);
friend ostream&operator问题:
您已声明,outFile
将具有外部链接。std::ofstream
没有默认构造函数
std::ofstream
有一个非默认构造函数,并接受两个参数。第一个参数是文件的位置,第二个参数(默认为btw)是打开/写入文件的方式
因此,您可以通过将流出流文件的外部;
替换为以下内容来修复此错误:
流输出文件(“output.txt”);
在矩形.cpp中
据我所知,无需在.cpp
文件中将其声明为extern
接下来,istream&operator>>(istream&input,矩形和网格)
不返回任何内容
在函数完成之前,它应该返回input
最后,main的()
括号后面缺少一个花括号
您的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”
int main()
{
矩形矩形矩形[10];
整数倍;
对于(int i=0;i>rect[i];
cout>(istream和input、矩形和网格);
朋友OsFrand和操作人员:请显示你的代码。你在链接中缺少C++运行时库。问题是你的命令行。你使用什么编译器?我怀疑它认为你在使用C而不是C++(因此没有合并C++库)。我使用微软Visual C++ 2010的表达,请把它切成一个新的SeZeI,这个网站是新的,一直在想如何用我看到的格式格式化它。欢迎来到StAdvExcel。网站本身没有格式化程序(我知道)。但是,如果您使用IDE,它应该具有格式化选项。例如,在代码块IDE中,您可以右键单击文件进行格式化,或者右键单击项目进行格式化。如果使用Netbeans,也可以右键单击格式。Visual Studio在其多个菜单(可能是“编辑”或“查看”菜单)中有一个菜单。
#include "Rectangle.h"
int main()
Rectangle rect[10];
int numRectangles;
for(int i = 0; i < numRectangles; i++)
{
cin >> rect[i];
cout << rect[i];
}
}
#include "Rectangle.h"
int main()
{
Rectangle rect[10];
int numRectangles;
for(int i = 0; i < numRectangles; i++)
{
cin >> rect[i];
cout << rect[i];
}
}
#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
#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;
}
#include "Rectangle.h"
int main()
{
Rectangle rect[10];
int numRectangles;
for(int i = 0; i < numRectangles; i++)
{
cin >> rect[i];
cout << rect[i];
}
}