首页 > 解决方案 > 类向量上的 C++ 编译问题

问题描述

我很难解决编译错误,如下所示。我目前正在研究可能会更改哪些内容以修复错误。谁能告诉我我需要改变什么?我的编译器指出的代码如下。问题也可能在生成文件中,但我不确定要包含什么。非常感谢任何帮助,谢谢!

编译错误:

 C:\cygwin64\home\user\cpplab7>nmake -f makefile.ms

Microsoft (R) Program Maintenance Utility Version 14.16.27026.1
Copyright (C) Microsoft Corporation.  All rights reserved.

    cl /Foms\\main.obj /W4 /WX /Za /EHsc /nologo /c main.cpp
main.cpp
c:\cygwin64\home\user\cpplab7\cs170_vector.h(101): error C2039: 'max': is 
not a member of 'std'
C:\Program Files (x86)\Microsoft Visual 
Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\iomanip(20): note: 
see declaration of 'std'
c:\cygwin64\home\user\cpplab7\cs170_vector.h(98): note: while compiling 
class template member function 'void cs170::vector<short>::push_back(const T 
&)'
    with
    [
        T=short
    ]
main.cpp(59): note: see reference to function template instantiation 'void 
cs170::vector<short>::push_back(const T &)' being compiled
    with
    [
        T=short
    ]
main.cpp(53): note: see reference to class template instantiation 
'cs170::vector<short>' being compiled
c:\cygwin64\home\user\cpplab7\cs170_vector.h(101): error C3861: 'max': 
identifier not found

生成文件:

# Macros ========================================
CC = cl
NOLGFLAG = /nologo
CFLAGS = /W4 /WX /Za /EHsc
OBJFLAG = /Fo
EXEFLAG = /Fe
OUTDIR = ms\\

SRC1 = main.cpp
SRC2 = 
HDR2 = 

OBJ1 = $(OUTDIR)main.obj

OBJS= $(OBJ2) $(OBJ1)

EXE = $(OUTDIR)out.exe
ERASE = rm
MAKEFILE = makefile.ms


# Targets ========================================

$(EXE) : $(OBJS)
$(CC) $(EXEFLAG)$(EXE) $(NOLGFLAG) $(OBJS)

$(OBJ1) : $(SRC1)
$(CC) $(OBJFLAG)$(OBJ1) $(CFLAGS) $(NOLGFLAG) /c $(SRC1)


clean :
-$(ERASE) $(OBJS) $(EXE)

rebuild :
-$(ERASE) $(OBJS) $(EXE)
-$(MAKE) -f $(MAKEFILE) -i

指向此函数的编译器:

#include <iostream>
#include <iomanip>
void push_back(const T& t)
{
    if(count+1>capacity)
{
    reserve(std::max(2 * capacity, 1));

    T* newData = new T[capacity];
    for(int i=0; i <count; i++)
    {
        newData[i] = v[i];
    }
    delete[] v;
    v = newData;
}
v[count++] = t;
}

标签: c++compilation

解决方案


std::max被定义在<algorithm>其中你没有包括在内。

#include <algorithm>

推荐阅读