我正试图从Deitel的书中做另一个练习.该程序计算每月利息并打印每个储户的新余额.由于练习是与动态内存相关的章节的一部分,我使用的是"new"和"delete"运算符.出于某种原因,我得到了这两个错误:
LNK2019:函数___tmainCRTStartup中引用的未解析的外部符号WinMain @ 16
致命错误LNK1120:1个未解析的外部因素
这是类头文件.
//SavingsAccount.h
//Header file for class SavingsAccount
class SavingsAccount
{
public:
static double annualInterestRate;
SavingsAccount(double amount=0);//default constructor intialize
//to 0 if no argument
double getBalance() const;//returns pointer to current balance
double calculateMonthlyInterest();
static void modifyInterestRate(double interestRate):
~SavingsAccount();//destructor
private:
double *savingsBalance;
};
Run Code Online (Sandbox Code Playgroud)
具有成员函数定义的Cpp文件
//SavingsAccount class defintion
#include "SavingsAccount.h"
double SavingsAccount::annualInterestRate=0;//define and intialize static data
//member at file scope
SavingsAccount::SavingsAccount(double amount)
:savingsBalance(new double(amount))//intialize savingsBalance to point to new object
{//empty body
}//end of constructor
double SavingsAccount::getBalance()const
{ …Run Code Online (Sandbox Code Playgroud) 我有abstract Base班级和Derived班级.
int main ()
{
Base *arrayPtr[3];
for (int i = 0; i < 3; i++)
{
arrayPtr[i] = new Derived();
}
//some functions here
delete[] arrayPtr;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何使用删除操作符.如果我删除了如上所示的基类指针数组,那么这会调用派生类对象的析构函数并清理内存吗?
不确定导致该问题的原因,但是当我运行查询时
select to_char(sysdate, 'HH24:MM') from dual;
Run Code Online (Sandbox Code Playgroud)
我经常在15:08得到同一时间.我甚至在不同的机器(也是Windows)上检查不同的Oracle DBMS并得到相同的结果.
我有一个User对象的ArrayList.现在我只需要这些用户名的ArrayList.有没有办法toString()在整个ArrayList 上使用并将其转换为String名称的ArrayList而不是在for循环中执行此操作?我也在User类中重写了toString,所以它返回了用户的名字,我试过ArrayList <String> names = usersList.toString()但它没有用.
我正在做另一个C++练习.我必须从无限系列计算pi的值:
pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 -4/11 +...
该程序必须在本系列的前1000个术语中的每一个之后打印pi的近似值.这是我的代码:
#include <iostream>
using namespace std;
int main()
{
double pi=0.0;
int counter=1;
for (int i=1;;i+=2)//infinite loop, should "break" when pi=3.14159
{
double a=4.0;
double b=0.0;
b=a/static_cast<double>(i);
if(counter%2==0)
pi-=b;
else
pi+=b;
if(i%1000==0)//should print pi value after 1000 terms,but it doesn't
cout<<pi<<endl;
if(pi==3.14159)//this if statement doesn't work as well
break;
counter++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它编译时没有错误和警告,但执行后只显示空控制台窗口.如果我删除行"if(i%1000 == 0)",我可以看到它确实运行并打印每个pi值,但它不会停止,这意味着第二个if语句也不起作用.我不知道还能做什么.我假设它可能是一个简单的逻辑错误.
我有一个Varchar日期列,我想选择日期列中的日期与系统中当前日期匹配的所有行.我想出了这个查询,但它没有找到任何匹配,虽然今天是星期四,并且星期四存储在日期列中.我不确定这是否是将日期数据类型与varchar进行比较的正确方法.
select * from schedule
where day = (select to_char(sysdate,'day') from dual);
Run Code Online (Sandbox Code Playgroud) 我需要过滤所有行,其中的单词以字母开头,后跟零个或多个字母或数字,但没有特殊字符(基本上可以用于c ++变量的名称).
egrep '^[a-zA-Z][a-zA-Z0-9]*'
Run Code Online (Sandbox Code Playgroud)
这适用于诸如"a","ab10"之类的单词,但它也包括像"bb"这样的单词.我明白*在表达结束时是*问题.如果我用+(一个或多个)替换*,它会跳过只包含一个字母的单词,所以它没有帮助.
编辑:我应该更精确.我想找到具有任意数量的可能单词的行,如上所述.这是一个例子:
int = 5;
cout << "hello";
//some comments
Run Code Online (Sandbox Code Playgroud)
在这种情况下,它应该打印上面的所有行,因为它们都包含至少一个符合所描述条件的单词,而行不必以字母开头.
当我尝试在Visual C++ 2008 Express上编译下面的代码时出现系统错误.我要做的是用文件读取数据初始化对象数组.我认为while循环中有一些错误,因为当我手动初始化这些对象时没有while循环它似乎工作.这是代码和文本文件:
#include <iostream>
#include <string>
#include "Book.h"
using namespace std;
int main()
{
const int arraySize = 3;
int indexOfArray = 0;
Book bookList[arraySize];
double tempPrice;//temporary stores price
string tempStr;//temporary stores author, title
fstream fileIn( "books.txt" );
while ( !fileIn.eof( ))
{
getline(fileIn,tempStr);
bookList[indexOfArray].setAuthor(tempStr);
getline(fileIn,tempStr);
bookList[indexOfArray].setTitle(tempStr);
fileIn >> tempPrice;
bookList[indexOfArray].setPrice(tempPrice);
if ( indexOfArray < arraySize ) //shifting array index while not exceeding array size
indexOfArray++;
}
fileIn.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
和文本文件:
Author1
Book1
23.99
Author2
Book2
10.99 …Run Code Online (Sandbox Code Playgroud) 该程序应该找到在Unix上输入的以".exe"结尾的命令行参数.由于某种原因,它不起作用.这是代码:
int main( int argc, char* argv[] )
{
for ( int i = 1; i < argc; i++)
if( findExe( argv[i] ) )
cout << argv[i] << endl;
return 0;
}
bool findExe( char* argument )
{
if ( strlen( argument ) >= 4 )
{
string testExe = ".exe";
string initialWord=argument; //converts c-string to string
string temp( initialWord,( initialWord.size() - 4 ),4 );//creates temp with last four characters from initialWord
if ( !temp.compare(testExe) )
return true;
}
else …Run Code Online (Sandbox Code Playgroud) 我只是想尝试在鼠标点击的同一位置绘制一个圆圈,但我无法让它工作.这是代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LineApp
{
public static void main ( String args[])
{
MainWindow mainWdw = new MainWindow();
}
}
class MainWindow extends Frame
{
private Point recPoint;
private boolean mouseClick;
MainWindow()
{
mouseClick = false;
setBackground( Color.BLACK );
setSize( 400,300 );
setLocation( new Point( 300,300));
addComponentListener(new ComponentCatcher());
addWindowListener(new WindowCatcher());
addMouseListener(new MouseCatcher());
setVisible( true );
}
public void paint( Graphics gc )
{
gc.setColor(Color.ORANGE);
if(mouseClick)
{
gc.setColor(Color.ORANGE);
gc.fillOval(recPoint.x, recPoint.y, 30,30);
}
}
class WindowCatcher …Run Code Online (Sandbox Code Playgroud) 这是我的代码:
double round( char* strNumber, int decPlace);
int main()
{
int decimal;
char initialNumber[256];
cout << "Enter decimal and number " << endl;
cin >> decimal;
cin >> initialNumber;
cout << setprecision (15) << round ( initialNumber,decimal ) << endl;
return 0;
}
double round( char* strNumber, int decPlace)//
{
double number = atof(strNumber);
int temp = ( int ) ( pow(10.0,decPlace) * number + 0.5 );
double result = ( double ) temp / pow(10.0,decPlace);
return result;
}
Run Code Online (Sandbox Code Playgroud)
它最多可以工作6位小数.否则会产生一些奇怪的结果.以下是我用于测试和输出的数字: …
我是Java初学者,但我认为在使用try-catch-finally时我不必使用声明异常throws SQLException.但是,如果我不使用它,编译器会给我错误:
"未报告的异常java.sql.SQLException;必须被捕获或声明被抛出".
我包含了一个catch,所以我不确定为什么会出现这种错误.
public static ResultSet getResultSet ( String query )
{
dbConn = getConnection();
try
{
stmt = dbConn.createStatement( );
ResultSet rs = stmt.executeQuery( query );
return rs;
}
catch (SQLException ex)
{
return null;
}
finally
{
stmt.close();
dbConn.close();
}
}
Run Code Online (Sandbox Code Playgroud)