| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3243 人关注过本帖
标题:新手学c++遇到的问题 ,百度不到啊!
只看楼主 加入收藏
自制力
Rank: 2
等 级:论坛游民
帖 子:19
专家分:32
注 册:2016-11-15
结帖率:60%
收藏
已结贴  问题点数:20 回复次数:11 
新手学c++遇到的问题 ,百度不到啊!
     第一个问题
 asdf.h 头文件
extern const int fingers=10;
/*****在c++里面const 这个代表为是内部链接,为了让fingers有外部链接  所以,我用了 extern*******/
const char*warning="walk!";

1.cpp源文件
#include
#include"asdf.h"
int abc()
{
extern   const int fingers;//声明!!
using namespace std;
extern int a;
cout<cout<return 0;
}


2.cpp 源文件2

#include
#include"asdf.h"
 extern int a=10;
 extern int abc();
int main()
{
extern const  int fingers;//声明!!
using namespace std;
int a=12;
cout<abc();
cout<cin.get();
return 0;

}


/**************************然后******************************

Linking...
2.obj : error LNK2005: "char const * const warning" (?warning@@3PBDB) already defined in 1.obj
2.obj : error LNK2005: "int const fingers" (?fingers@@3HB) already defined in 1.obj
Debug/看看extern.exe : fatal error LNK1169: one or more multiply defined symbols found
执行 link.exe 时出错.***************************链接的时候错啦,什么鬼??****************************************/




第二个问题   new  的定位功能
#include
#include

const int B = 512;
const int N = 1;
char buffer[B];
int main()
{
    using namespace std;
    double *a;
    double *b;
    a = new double[20];
    b = new (buffer)[N];   //  vs2013 说我 这里错啦[应该要输入类型说明符
    *b = 'A';
    a[0] = 10.0;
    int i;
    for (i = 1; i<19; i++)
    {
        a[i] += a[i - 1];

    }
    cout << "address:" << a << endl;
    cout << "b的地址是" << b << endl;
    cout << "buffer的地址是:" << (void*)buffer;
    delete[] a;
    delete[] b;
    return 0;
}


第三个问题
这个  问题 很神奇 我很喜欢!!
#include
#include
const int B=512;
int buffer[B];
int main()
{
    using namespace std;
    double *pd1;
    double *pd2;
    pd1=new double ;
    pd2=new (buffer) double ;
       *pd1=120.2;
       *pd2=13.13;
    cout<<*pd1<<"at"<    cout<<*pd2<<"at"<    cout<<"buffer"<<"at"<    cin.get();
    return 0;
}
**********************             ********************
  1,为什么 pd2是double的指针而,buffer 是int 的指针  他们在程序中显示的还是同样?
,2,当  char  buffer   那么  要(void *)buffer  为什么不是  (double *)buffer?

 
*******************              ************


第四个问题
*****************************************************************
#include
#include
#include
using namespace std;
const int S = 4;
const arraysnames =
{ "spring", "summer", "fall", "winter" };
void fill(array*pa);
void show(arrayda);

int main()
{
    array expenses;
    fill(&expenses);
    show(expenses);//********说我没有标识show(expenses)。。***   我明明标识了....
    return 0;
}
void fill(array*pa)
{
    for (int i = 0; i < S; i++)
    {
        cout << "enter " << snames[i] << "expenses";
        cin >> (*pa)[i];

    }
}
void show(arrayda)
{
    double total = 0.0;
    cout << "\nexpenses" << endl;
    for (int i = 0; i < S; i++)
    {
        cout << snames[i] << ":$" << da[i] << endl;
        total += da[i];

    }
    cout << "total expenses :$" << total << endl;


}


第五个问题  函数指针
 **********************************************
#include
using namespace std;
void abc( int z ,int x,   void*pf)(int&a,int&b));
void change(int&a ,int&b);

int main()
{
   
    int x = 12;
    int y = 123;
    abc(x, y, change)
        cout << y << endl;
        cout << x << endl;
        cin.get();
        return 0;
   

}

void change(int &a, int&b)
{
    int t;
    t = a;
    a = b;
    b = t;
}
void abc(int z,int x, void(*pf)(int&a, int&b))
{
    cout << ".....";
    (*pf)(z,x)
}
****************************错了很多啊*******搞不懂***********





第六个问题  template 和 typename



********************还是懵逼啊*************运行环境vs2013***
#include
using namespace std;
template
void swap(T &a, T&b);

int main()
{
   
    int i = 10;
    int j = 20;
    cout << "i,j=" << i << "," << j << endl;
    cout << "using compier generated int sseapper " << endl;
    swap(i,j);
    cout << "now i,j=" << i << "," << j << endl;

    double x = 24.5;
    double y = 81.7;
    cout << "x,y=" << x << "," << y << endl;
        cout << "uisng ";
    swap(x,y);
    cout << "now ,x,y=" << x << "," << y<< endl;
    cin.get();
    cin.get();
    return 0;
}

template
void swap(T&a, T&b)
{
    T temp;
    temp = a;
    a = b;
    b = temp;
}



同样的问题!!  
#include
template
void swap(T &a, T &b);
template
void swap(T *a, T *b, int n);
void show(int a[]);
const int L = 8;
int main()
{
    using namespace std;
    int i = 10, j = 20;
    cout << "i,j=" << i << "," << j << endl;
    cout << "using " << endl;
    swap(i, j);
    cout << "i,j=" << i << "," << j << endl;


    int d1[L] = { 0, 75, 7, 5, 75, 756, 762, 546 };
    int d2[L] = { 1, 64, 64, 64, 74, 64, 3, 6, };
    cout << "oringinal arrays" << endl;
    show(d1);
    show(d2);
    cin.get();
    return 0;
}
template
void swap(T &a, T&b)
{
    T temp;
    temp = a;
    a = b;
    b = temp;
}
template
void swap(T a[], T b[], int n)
{
    T temp;
    for (int i = 0; i < n; i++)
    {
        temp = a[i];
        a[i] = b[i];
        b[i] = temp;
    }
}

void show(int a[])
{
    using namespace std;
    cout << a[0] << a[1] << endl;
    cout << a[2] << a[3] << endl;
    for (int i = 4; i < L; i++)
        cout << a[i];
    cout << endl;
}

说什么:    2    IntelliSense:  有多个 重载函数 "swap" 实例与参数列表匹配:
            函数模板 "void swap(T &a, T &b)"
            函数模板 "void std::swap(_Ty &, _Ty &)"
            参数类型为:  (int, int)    c:\mycode\c++\c++ primer plus\函数探\代码\函数\函数\函数.cpp    306    2    函数
/**********说好的可以自己判断的啊!!我的重载没有冲突问题啊******/

错误    1    error C2668: “std::swap”: 对重载函数的调用不明确    c:\mycode\c++\c++ primer plus\函数探\代码\函数\函数\函数.cpp    306    1    函数

还是这个问题啊


 我要疯了!!!,错误的提示一模一样!!,难道vs2013没有c++语言链接性了??
#include
template
void swap(T &a, T &b);
struct job
{
    char name[40];
    double salary;
    int floor;
};
template <> void swap(job&j1, job&j2);
void show(job &j);
int main()
{
    using namespace std;
    cout.precision(2);
    cout.setf(ios::fixed, ios::floatfield);
    int i = 10;
    int j = 20;
    cout << "i,j=" << i << "," << j << endl;
    job sue = { "susan yaffee", 73000.60, 7 };
    job sidney = { "sidney taffee", 7860.73, 9 };
    cout << "before job swapping" << endl;
    show(sue);
    show(sidney);
    swap(sue, sidney);
    cout << "after job swapping" << endl;
    show(sue);
    show(sidney);
    cin.get();
    return 0;
}
template <>void swap(job&j1, job&j2)
{
    double t1;
    int t2;
    t1 = j1.salary;
    j1.salary = j2.salary;
    j2.salary = t1;
    t2 = j1.floor;
    j1.floor = j2.floor;
    j2.floor = t2;
}

void show(job&j)
{
    using namespace std;
    cout << j.name << ":$" << j.salary
        << "on floor" << j.floor << endl;
}
*****************************************************************

第 七个问题 ..string  和 char *  的转换

#include
//#include
#include
int main()
{
    using namespace std;
    cout << "ssss" << endl;
    string abc="sdfh";
    int n = 10;
const char *p = abc.c_str();//  把const加上,cin.getline()有问题
    cin.getline(p, n);//不加const  char*p=abc.c_str()有问题
}

第八个问题    引用和string
#include
//#include"duixiang.h"
 void  getnamme(  std:: string &a, int n)
{
        using namespace std;
        cout << "please input your name" << endl;
        int i;
        for (i = 0; i < n; i++)
            cin >> a[i];
        cout << a;/// **********
怎么就有错误了??a是一个string 类型的字符串引用!干嘛说我错!!
}



第九个问题       下面是一个头文件
#ifndef S
#define S
#include
class atm
{
private:
       std::vector name(20);//说我(20)这里要插入类型说明符
public:
    void getname(std::vector &a, int n);//说我std::vector缺少类模板的参数列表。。。为什么啊
};
#endif


*******************************************************
const Stock&topval(const Stock& S) const/*说我非成员函数上不可以用                                  const限定  后面的const明明   限定this指针的,不让它改变值的*/
{
    if (S.total_val > total_val)
//说我S.要什么输入表达式。。。。
        return s;
    else
        return *this;
}

***********************************




搜索更多相关主题的帖子: fingers include warning 百度 return 
2016-12-15 22:09
自制力
Rank: 2
等 级:论坛游民
帖 子:19
专家分:32
注 册:2016-11-15
收藏
得分:0 
请 好心人 解惑。。。我加了好几个群  都没什么人愿意去告诉我到底怎么回事   我学c++不久。。。。
2016-12-15 22:10
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:0 
第一题,如果 extern const int fingers=10; 是个声明,那为什么还有 =10 ?
在.h中声明 extern const int fingers; 在任意.cpp中定义 const int fingers = 10;

const char* warning = "walk!";
这句话说的是 warning 指向的内容是 const 属性,
所以应该是 const char* const warning = "walk!";
2016-12-16 08:31
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:0 
第二题
b = new (buffer)[N];
我也看不懂这是什么,你是placement new一个 char数组内,还是int数组,还是double数组,还是…… ?
加上类型,即 b = new(buffer) double[N];
2016-12-16 08:34
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:0 
第三题
1. new(buffer) double; 的意思是 在 buffer所指内存处 构造一个double类型变量
2. 听不懂
2016-12-16 08:37
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:0 
第4题,你写的是什么玩意儿,完全看不懂,多处使用了 array 这个类型,可是却没有 array 的类型定义。
2016-12-16 08:42
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:0 
第5题,基础知识你还是多看书吧

程序代码:
#include <iostream>

void abc( int z ,int x, void (*pf)(int&a,int&b) );
void change( int& a, int& b );

using namespace std;

int main( void )
{
    int x = 12;
    int y = 123;
    abc( x, y, change );
    cout << y << '\n';
    cout << x << endl;
    return 0;
}

void change( int& a, int& b )
{
    int t = a;
    a = b;
    b = t;
}

void abc( int z ,int x, void (*pf)(int&a,int&b) )
{
    cout << "......\n";
    (*pf)( z, x );
}

2016-12-16 08:46
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:0 
第6题,你的 swap,和 std::swap 重名了
2016-12-16 08:49
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:0 
第7题,
c_str() 的返回类型是 const char*
getline第一个参数类型是 char*

2016-12-16 08:51
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:0 
第8题,不知所云,我怀疑你未加 #include <string>
2016-12-16 08:53
快速回复:新手学c++遇到的问题 ,百度不到啊!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019131 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved